Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #762 > unrolled thread

Please comment on my first parser

Started byBart Vandewoestyne <bart.vandewoestyne@gmail.com>
First post2012-10-10 06:28 -0700
Last post2012-10-13 20:50 -0700
Articles 2 — 2 participants

Back to article view | Back to comp.compilers


Contents

  Please comment on my first parser Bart Vandewoestyne <bart.vandewoestyne@gmail.com> - 2012-10-10 06:28 -0700
    Re: Please comment on my first parser Jim Patchell <patchell@cox.net> - 2012-10-13 20:50 -0700

#762 — Please comment on my first parser

FromBart Vandewoestyne <bart.vandewoestyne@gmail.com>
Date2012-10-10 06:28 -0700
SubjectPlease comment on my first parser
Message-ID<12-10-004@comp.compilers>
Hello all,

Just as a hobby project, I am working my way through Appel's 'Modern Compiler
Implementation in C'.  I have now finished Chapter 3 and it looks like my
parser for the Tiger language can parse all test examples from the book.

Nevertheless, I want to learn from this experience and improve my skills.  It
would therefore be nice to get some more feedback from experts.
Comments/suggestions on my grammar rules, C-style, code-layout (indentation
etc),... are highly appreciated.  Also, test-programs that reveal bugs in my
parser are welcome.

One thing I am not completely sure of is whether i solved the shift-reduce
problems correctly by adding some nonassoc lines for the DO, OF, THEN and ELSE
tokens... please let me know if this is the correct way to do it or not.


My grammar file and all other code for Chapter 3 can be found at:

https://github.com/BartVandewoestyne/c/blob/master/books/Modern_Compiler_Impl
ementation_in_C/chap03/tiger.grm

https://github.com/BartVandewoestyne/c/tree/master/books/Modern_Compiler_Impl
ementation_in_C/chap03

Thanks!
Bart

[toc] | [next] | [standalone]


#766

FromJim Patchell <patchell@cox.net>
Date2012-10-13 20:50 -0700
Message-ID<12-10-008@comp.compilers>
In reply to#762
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

expression
        : INT
        | STRING
        | BREAK
        ... etc

and then later on you have...

arithmetic_expression
        : 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

BREAK + BREAK

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

primary :IDENTIFIER
        |CONSTANT
        | STRING
        | LPAREN, expression, RPARAN

etc.....

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 ( http://www.parsifalsoft.com/ )
and LRstar (http://www.compilerware.com) seems to be offline at the moment :-(
and ANTLR ( http://www.antlr.org/ )

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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.compilers


csiph-web