Skip to content

Conversation

@cmaglie
Copy link
Contributor

@cmaglie cmaglie commented Dec 26, 2024

In the example exposed in #151:

%%

S -> A;
A -> "a" | ;
Parsing mode: LL(1).

Grammar:

    1. S -> A
    2. A -> 'a'
    3.    | ε

we get the wrong LL1 Parsing Table:

LL(1) parsing table:

┌───┬─────┬───┐
│   │ "a" │ $ │
├───┼─────┼───┤
│ S │ 1   │   │
├───┼─────┼───┤
│ A │ 2   │ 3 │
└───┴─────┴───┘

The problem is in the condition that checks the existence of ε, because a production may have other terminals besides ε.
In our case, the First(A) set equals to "a", ε. When we fill the table we should consider "a" and replace ε with the Follow(S) set.

After this patch we get the expected result:

LL(1) parsing table:

┌───┬─────┬───┐
│   │ 'a' │ $ │
├───┼─────┼───┤
│ S │ 1   │ 1 │
├───┼─────┼───┤
│ A │ 2   │ 3 │
└───┴─────┴───┘

Should fix #151

Copy link
Owner

@DmitrySoshnikov DmitrySoshnikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for fixing it! Added one comment on the code.

@cmaglie
Copy link
Contributor Author

cmaglie commented Jan 2, 2025

Looks good, thanks for fixing it!

Thank you for making such good courses!

@DmitrySoshnikov DmitrySoshnikov merged commit 7a628f4 into DmitrySoshnikov:master Jan 3, 2025
@cmaglie cmaglie deleted the ll1-table-gen-fix branch January 3, 2025 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug in LL(1) parse table

2 participants