[comp.archives] [comp.compilers] Re: Can Pascal be parsed by LR

joel@decwrl.dec.com (10/11/90)

Archive-name: modula-2-grammar/10-Oct-90
Original-posting-by: joel@decwrl.dec.com
Original-subject: Re: Can Pascal be parsed by LR(1) parsing algorithm?
Archive-site: gatekeeper.dec.com [16.1.0.2]
Archive-directory: /pub/DEC/Modula-2
Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)

Yacc is really a crude tool for parser construction.  After much
experimentation, I got yacc to not only accept Modula-2's statements, but
to report when you forgot to put in a missing semicolon as well.  I didn't
do the same for Pascal, as it was too much work.  You can find complete
grammars in the Modula-2/Pascal distribution available for anonymous ftp
from gatekeeper.dec.com, file /pub/DEC/Modula-2/m2.vax.tar.Z.  Here's the
relevant code for Modula-2, just to let you know the horrors of yacc
grammar c onstruction.


/* blame yacc for gross sequence syntax */
StatementSequence:
                StatementSequence1
                                /**/
        |       semis StatementSequence1 semis
                                { $$ = $2; }
        |       StatementSequence1 semis
                                /**/
        |       semis StatementSequence1
                                { $$ = $2; }
        |       semis
                                { $$ = AddToStmtList(0,0); }
        |       /* empty */
                                { $$ = AddToStmtList(0,0); }
        ;

StatementSequence1:
                StatementSequence1 semis statement
                                { $$ = AddToStmtList($1,$3); }
        |       StatementSequence1 { yyerror("Missing semicolon"); } statement
                                { $$ = AddToStmtList($1,$3); }
        |       statement
                                { $$ = AddToStmtList(0,$1); }
        ;

semis:
                SEMICOLON
        |       semis SEMICOLON
        ;



-- 
- Joel McCormack (decwrl!joel, joel@decwrl.dec.com)
-- 
Send compilers articles to compilers@esegue.segue.boston.ma.us
{ima | spdcc | world}!esegue.  Meta-mail to compilers-request@esegue.