Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing syntax analysis of "if-else" constructs #7

Open
malharjajoo opened this issue Feb 21, 2016 · 1 comment
Open

Implementing syntax analysis of "if-else" constructs #7

malharjajoo opened this issue Feb 21, 2016 · 1 comment

Comments

@malharjajoo
Copy link

In order to implement the syntax analysis of "if-else" construct , don't we need to check if the number of "elses" are less than or equal to ( but never more) than the number of "ifs" ?

How can we achieve this in the grammar ( without using something like a stack ? ) ?

@m8pple
Copy link
Contributor

m8pple commented Feb 25, 2016

As long as you write something like:

IfStat : TIf TLParen Expr TRParen Stat TElse Stat

then the grammar itself enforces it. If it encounters an 'else'
that doesn't match an if, then it would be a syntax error.

I believe the only place that 'else' can be found is in an
if statement, so anywhere else it wouldn't match anything.

In terms of nested ifs and elses, then we have the dangling
else situation. The bison documentation has some information
on how to handle that:
http://www.gnu.org/software/bison/manual/html_node/Shift_002fReduce.html

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

No branches or pull requests

2 participants