0

I am studying compiler design and I am trying to implement an LL(1) parser by hand. I already finished the lexical scanner and it works great. However, I'm quite new to elimination of left recursiveness and left factoring. I created a parse() method in java and it is outputting a stackoverflow. I think it is caused by infinite recurse of something in my grammar. What might be causing it? Here's the grammar I'm using:

start      -> assign_var statement
assign_var -> DATATYPE VARIABLE ASSIGNMENT var assign_var | eps
var        -> expression | STRING
expression -> factore'
e'     -> PLUSMINUS factore' | eps
factor     -> gf' 
f'     -> MULDIV gf' | eps
g      -> VARIABLE | NUMBER | DECIMAL
statement  -> STARTIF IF boolean_stmt THEN statement butif_stmt FINISHIF
statement  -> STARTFROM FROM assign_var UNTIL boolean_stmt LOOP statement FINISHFROM
statement  -> expression statement
statement  -> WRITE g
statement  -> READ g
statement  -> eps
butif_stmt -> BUTIF boolean_stmt THEN statement | eps
boolean_stmt -> expression bln_optr expression
bln_optr   -> EQUALITY | INEQUALITY | LESSTHANGREATERTHANEQUAL

where the capital letter words are the terminal symbols. Thank you!

Here's a bit of the java code:

 public static void assign_var(){
    //assign_var -> DATATYPE VARIABLE ASSIGNMENT var assign_var | eps
    if (lookahead.equals("DATATYPE")){
        nextToken();

        if(lookahead.equals("IDENTIFIER")){
            nextToken();

            if(lookahead.equals("ASSIGNMENT")){
                nextToken();

                var();  
                assign_var();
            }

        }
    }
    else {} //epsilon
}

The else statement is where I'm planning to implement the eps

  • 1
    My bet goes on the second line. `assign_var ->` pointing to the same assign_var latter on. Just a thought though. Without any code it is hard to say for sure. – Jorge Campos Apr 24 '18 at 00:54
  • Hi, @JorgeCampos thanks for your quick response. In the java file it is returning stackoverflow because it can't escape the recursion as you said. However, I'm having trouble in implementing the epsilon where in grammar, it is the empty symbol. Do you know any workarounds in order to implement it? – Paul Steven Fantonalgo Nadera Apr 24 '18 at 00:59
  • 1
    Unfortunately not. My knowledge on grammars are very sparse. Maybe if you show your code and add details of what is happening and what should be happening I may be of some help. – Jorge Campos Apr 24 '18 at 01:01
  • 1
    Click on the [edit](https://stackoverflow.com/posts/49991810/edit) at the bottom of your question and add your code formatted. add a blank line before the code and add four empty spaces before every line. – Jorge Campos Apr 24 '18 at 01:04
  • 2
    Not in the comments. In your question. The edit button is right below the tags. – Jorge Campos Apr 24 '18 at 01:09
  • My money is on `statement -> expression statement`. Right-recursion will occupy all the stack and more given the chance. You need to refactor this. – user207421 Apr 24 '18 at 03:21
  • @EJP Yes the grammar is very messy and sloppy coding. Maybe due to lack of sleep. I already fixed it. Maybe I'll edit the code later. Thank you! – Paul Steven Fantonalgo Nadera Apr 24 '18 at 03:45

0 Answers0