Questions tagged [dangling-else]

15 questions
3
votes
1 answer

LL(1) grammar for dangling else

In compiler construction, one of the main ambiguity problems is dangling else. As mentioned in Compilers: Principles, Techniques, and Tools book by Aho, Lam, Sethi and Ullman, the grammar for the dangling else can't be used with LL(1) parsers. Is it…
HATEM EL-AZAB
  • 331
  • 1
  • 3
  • 11
2
votes
2 answers

Removing Ambiguity Caused By Dangling Else For LL(1) Grammars

In the case of the dangling else problem for compiler design, is there a reason to left factor it before removing ambiguity? We are transforming a CFG into an LL(1) grammar so my professor is asking us to first eliminate recursion, then left factor,…
2
votes
0 answers

LL1 and unambiguous grammar for dangling else

I am trying to write a simple compiler using Flex for scanner and a special tool named PGen to define grammars. Now, I am trying to solve unambiguous grammar for dangling else. I have searched for this. This is a grammar with dangling else…
Linda
  • 251
  • 1
  • 6
  • 16
1
vote
1 answer

How does the latest ANTLR4 resolve the "dangling else" ambiguity?

I am using antlr 'org.antlr:antlr4:4.9.2' and come across the "dangling else" ambiguity problem; see the following grammar IfStat.g4. // file: IfStat.g4 grammar IfStat; stat : 'if' expr 'then' stat | 'if' expr 'then' stat 'else' stat |…
hengxin
  • 1,867
  • 2
  • 21
  • 42
1
vote
1 answer

Solving dangling if, elsif, and else in Bison without associativity declarations

I am implementing a parser for a language that has if-elsif-else statements and can't seem to make my grammar unambiguous. Our compilers class was given a handout on solving the dangling else problem for if-else statements using a match/unmatched…
1
vote
1 answer

If statement to tree representation in ANTLR

I have the following if statement that parses correctly: ifStatement : 'IF' expression 'THEN' statementBlock (options {greedy=true;} : 'ELSE' statementBlock)? ; Now, I want to parse this into an AST. This is how I did…
pantelis
  • 113
  • 10
1
vote
0 answers

Dangling else grammar for Lex and Yacc

I'm writing a small parser to recognize a subset of Java and I've run into an issue that I believe is called the dangling else problem. My grammar for matching if-else statements started off like this: statement: block | emptystatement | ifstatement…
momonkey
  • 41
  • 1
  • 7
0
votes
0 answers

Bison Difficult Dangling Else Removal

I am trying to get rid of dangling else in my grammar. Terminals are capital-letter words. I have: statement: expression_statement (not important now ) | compound_statement (not important now ) | selection_statement |…
Riomare
  • 65
  • 1
  • 10
0
votes
1 answer

Beaver parser generator shift-reduce conflicts connected to dangling else

I am feeding a (generated) grammar to Beaver parser generator. Multiple shift-reduce conflicts are caused by it seems like the dangling else problem in this rules: Condition = IF LPAR Expression.expression RPAR Statement.trueStatement…
0
votes
1 answer

Bison Flex reduce/reduce conflict on dangling else with mid action

I am currently moving a fun-project of mine over to bison/flex as parser and have trouble solving a reduce/reduce conflict: // https://github.com/X39/yaoosl/blob/master/code-gen/yaoosl.y#L761-L766 ifthen: YST_IF YST_ROUNDO expression code_ifstart…
X39
  • 789
  • 6
  • 22
0
votes
1 answer

How to solve dangling else in Coco/R?

I have a dangling-else problem in Coco/R. I try to understand the Coco/R User Manual and I ask Google, but I can't solve the problem on my own. I simplified my problem to the following Coco/R grammar (saved in new4.atg): COMPILER Expr CHARACTERS …
Eric
  • 1
  • 1
-1
votes
3 answers

Curly Braces (scope) related unexpected output of if-elseif loop in C++

I have used a normal if-elseif ladder to update some variables depending upon conditions. The condition can be either a, b or c. That's why I am using an if-elseif ladder. But, when I use the if-elseif ladder without the curly braces, I'm not…
-1
votes
2 answers

Dangling else query (or an excercise in reading bad code)

The book I'm reading (C How to Program with an into to C++ Global Edition, Deitel&Dietel, 2016) gives the following code: Note that this is how the book presents the code in the exercise section, without braces and indentation on purpose. I would…
PerfectContrast
  • 144
  • 2
  • 2
  • 14
-1
votes
1 answer

Dangling else in Kotlin

I have this doubt guys, I don't know; any example to understand will be great. Does the Kotlin programming language suffer from the "dangling else" problem? If the problem is there then what is the reason in that case?
Adam
  • 21
  • 4
-7
votes
1 answer

What is exactly dangling-else problem in c?

How can the code work like this? Which if-else statements are linked with each other? So why is the output like that "$$$$$"? #include int main() { int x = 11; int y = 9; if(x<10) if(y>10) puts("*****"); else …
Atakan
  • 1
  • 1