Tanya
Tanya

Reputation: 21

writing yacc grammar for parsing these statements

i want to write yacc grammar for parsing statements as follows

START  
statement1
statement2  
END  
START
statement3
END
START
statement4
END

or

START
  statement1
  statement2
  XYZ               (2)
      START
      statement3
      statement4
      XYZ           (1)
      END
  statement5
  XYZ               (3)
END

and so on..

there can be any number of such START-END sets.

Each START should have an END.if XYZ appears at (1) then it should be present at (2) and (3) also. however if XYZ is present at (2) and(3) it may or may not be present at (1).that means the innermost START-END set may or may not have XYZ .. how should i write the grammar for such a pattern?

Upvotes: 0

Views: 152

Answers (1)

Emadpres
Emadpres

Reputation: 3737

Use this:

Program -> S

S -> S START statements END | ;

statements -> statements statement | ;

statement -> NormalStatement | XYZ ;

Now just use a counter to ensure the XYZ existence pattern in your grammar . You can check It in front of END in yacc file as you may know how.

Upvotes: 1

Related Questions