Re: Please comment on my first parser

Jim Patchell <>
Sat, 13 Oct 2012 20:50:14 -0700 (PDT)

          From comp.compilers

Related articles
Please comment on my first parser (Bart Vandewoestyne) (2012-10-10)
Re: Please comment on my first parser (Jim Patchell) (2012-10-13)
| List of all articles for this month |

From: Jim Patchell <>
Newsgroups: comp.compilers
Date: Sat, 13 Oct 2012 20:50:14 -0700 (PDT)
Organization: Compilers Central
References: 12-10-004
Keywords: parse
Posted-Date: 14 Oct 2012 17:35:18 EDT

On Wednesday, October 10, 2012 6:28:33 AM UTC-7, Bart Vandewoestyne wrote:
> [ a parser]

Hello Bart,

Well, it is nice to know there are other parser/compiler hobbyists out there besides myself.

I think your grammar has a few problems in it, however.

You start off with:

program: expression

                : INT
                | STRING
                | BREAK
                ... etc

and then later on you have...

                : expression PLUS expression
                | expression MINUS expression
                | expression TIMES expression
                | expression DIVIDE expression

If you BREAK production is some sort of a program flow control you could write


as a legal arithmetic_expresion.

I don't thing you meant that.

This is how I would start your grammar:

program: statements, eof //this means we have a list of statements followed by end of file

statements: statment //this is a left recursion rule for
                    | statements, statement //making a list of statements

statement: declarations
                  | looping_statements
                  | conditional_statments
                  | assignments

assignments: lvalue EQUALS expression

expression: expression MUL addexpr
                    | expression DIV addexpr
                    | addexpr

addexpr: addexpr PLUS urnary
              | addexpr MINUS urnary
              | urnary

urnary : primary
              | MINUS primary

                | STRING
                | LPAREN, expression, RPARAN


I sure hope I didn't make any grievous errors in that. There is a lot to be filled in also, Obviously.

Also, your grammar looks like it was written for either yacc or bison. Those are OK, but, there are better parser generators out there, and by better, I mean easier to use.

Take a look at Anagram ( )
and LRstar ( seems to be offline at the moment :-(
and ANTLR ( )

I haven't actually really tried ANTLR yet, but it is a very well documented package. Anagram and LRstar are my two fav LALR(1) parser generators. LRstar will even do LALR(k). ANTLR generates the so called LL(k) parsers.

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.