next up previous
Next: A starting point for symbol Up: Compiler Theory: Assignment 3 Previous: Implementing translation schemes with YACC

A starting point for the parser

We give a first version of a parser for MOOL programs Observe that The above files are called mool-0.l and mool-0.y in the archive yacc.tar.gz available from the course web site.

%{
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
%}

CLASS           class
INTEGER         int
NUM             [0-9]+
iDENTIFIER      [a-z][0-9a-zA-Z]*
IDENTIFIER      [A-Z][0-9a-zA-Z]*
LP              "("
RP              ")"
LB              "{"
RB              "}"
SM              ";"

%%


{CLASS}         {
                        strcpy(yylval.sval, yytext);
                        return TOK_CLASS;
                }
{INTEGER}       {
                        strcpy(yylval.sval, yytext);
                        return TOK_INT;
                }
{iDENTIFIER}    {
                        strcpy(yylval.sval, yytext);
                        return TOK_iD;
                }
{IDENTIFIER}    {
                        strcpy(yylval.sval, yytext);
                        return TOK_ID;
                }
{NUM}           {
                        yylval.ival= atoi(yytext);  
                        return TOK_NUM;
                }
{LP}            {
                        strcpy(yylval.sval, yytext);
                        return TOK_LP;
                }
{RP}            {
                        strcpy(yylval.sval, yytext);
                        return TOK_RP;
                }
{LB}            {
                        strcpy(yylval.sval, yytext);
                        return TOK_LB;
                }
{RB}            { 
                        strcpy(yylval.sval, yytext);
                        return TOK_RB;
                }
{SM}            {
                        strcpy(yylval.sval, yytext);
                        return TOK_SM;
                }
[ \t\n]+          /* eat up whitespace */

.               {
                        printf( "Unrecognized character: %s\n", yytext );
                }
%%


/* The following two definitions are needed under Linux */

int yyerror (char *s) {
printf ("%s\n", s);
}

int main(void) { 
yyparse(); 
}

Observe or recall that

%{
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int att = 0;

char theClass[256];

int yywrap()
{
  return 1;
}


%}

%union{
        int  ival;
        char sval[256];
      };


%token <ival> TOK_NUM
%token <sval> TOK_CLASS TOK_INT TOK_iD TOK_ID TOK_LP TOK_LB TOK_RP TOK_RB TOK_SM

%%
program         :            {printf("Parsing program ... \n"); } 
                  classDecs  
                             {printf(" Program parsed.\n");}
                ;


classDecs       : classDec  classDecs   
                | /* empty word */
                ;

classDec        : TOK_CLASS 
                  TOK_ID
                             {strcpy(theClass, $2);
                              printf("   Parsing class %s ...\n", theClass); 
                              att = 1; } 
                  TOK_LB 
                  attributes 
                  TOK_RB 
                            {printf("   Class parsed.\n"); }
                ;
attributes      :  attribute attributes
                | /* empty word */
                ;

attribute       :            {printf("      Attribute %d of class %s ", 
                                     att, 
                                     theClass) ; } 
                   TOK_INT
                   TOK_iD 
                            {printf("has type %s \n", $2);}
                   TOK_SM 
                             { att++; }
                 |            {printf("      Attribute %d of class %s ", 
                                     att, 
                                     theClass) ; } 
                   TOK_ID
                   TOK_iD 
                            {printf("has type %s \n", $2);}
                   TOK_SM 
                             { att++; }
                 ;

%%

[moreno@iguanodon yacc]$ make mool
------------------------------------------------------------
Building mool in /home/moreno/src/Courses/CS447/yacc/bin
/home/moreno/src/Courses/CS447/yacc/src/mool.l: In function `yyerror':
/home/moreno/src/Courses/CS447/yacc/src/mool.l:74: warning: control reaches end of non-void function
/home/moreno/src/Courses/CS447/yacc/src/mool.l: In function `main':
/home/moreno/src/Courses/CS447/yacc/src/mool.l:77: warning: implicit declaration of function `yyparse'
/home/moreno/src/Courses/CS447/yacc/src/mool.l:78: warning: control reaches end of non-void function
/home/moreno/src/Courses/CS447/yacc/src/mool.l: At top level:
lex.yy.c:1064: warning: `yyunput' defined but not used
y.tab.c: In function `yyparse':
y.tab.c:234: warning: implicit declaration of function `yylex'
y.tab.c:275: warning: implicit declaration of function `yyerror'
------------------------------------------------------------
[moreno@iguanodon yacc]$ more test/p1.mool 
class Pair {
        int left;
        int right;
}

class ThreeByTwo {
        Pair left;
        Pair middle;
        Pair right;
}

[moreno@iguanodon yacc]$ ./bin/mool < ./test/p1.mool 
Parsing program ... 
   Parsing class Pair ...
      Attribute 1 of class Pair has type int 
      Attribute 2 of class Pair has type int 
   Class parsed.
   Parsing class ThreeByTwo ...
      Attribute 1 of class ThreeByTwo has type Pair 
      Attribute 2 of class ThreeByTwo has type Pair 
      Attribute 3 of class ThreeByTwo has type Pair 
   Class parsed.
 Program parsed.


next up previous
Next: A starting point for symbol Up: Compiler Theory: Assignment 3 Previous: Implementing translation schemes with YACC
Marc Moreno Maza
2004-12-01