Top > TigerBook > PROGRAM3
Counter: 2373, today: 1, yesterday: 0

Chapter 3 の PROGRAM

PROGRAM

いろいろ手探りでやった結果以下の通りになった。 シフト還元衝突が一つだけ残っている。%change などの option 作業は行っていない。

%%
%term
    EOF
  | ID of string
  | INT of int | STRING of string
  | COMMA | COLON | SEMICOLON | LPAREN | RPAREN | LBRACK | RBRACK
  | LBRACE | RBRACE | DOT
  | PLUS | MINUS | TIMES | DIVIDE | EQ | NEQ | LT | LE | GT | GE
  | AND | OR | ASSIGN
  | ARRAY | IF | THEN | ELSE | WHILE | FOR | TO | DO | LET | IN | END | OF
  | BREAK | NIL
  | FUNCTION | VAR | TYPE
  | UNARYMINUS
%nonterm  exp | program
	| lvalue
	| sequensing | expsemilist
	| functionCall | explistopt | explist 
	| opExpr
	| recordCreate | typeid | idExpList | recField
	| arrayCreate | assignment | ifThenElse | ifThen
	| whileClause | forClause
	| letexp | expseq
	| parentheses 
	| decs | dec
	| tydec | ty | tyfields | tyfieldlist | tyfield
	| vardec | fundec

%pos int
%verbose
%start program
%eop EOF
%noshift EOF

%name Tiger
%keyword WHILE FOR TO BREAK LET IN END FUNCTION VAR TYPE ARRAY IF THEN ELSE
	DO OF NIL

%prefer THEN ELSE LPAREN

%value ID ("bogus")
%value INT (1)
%value STRING ("")

%nonassoc ASSIGN
%left DO THEN ELSE OF
%left OR
%left AND
%nonassoc EQ NEQ LT LE GT GE
%left PLUS MINUS
%left TIMES DIVIDE
%left UNARYMINUS

%%
program	: exp        ()

(***** Expressions *****)
exp: lvalue          ()
   | NIL             ()
   | sequensing      ()
   | INT             ()
   | STRING          ()
   | functionCall    ()
   | opExpr          ()
   | recordCreate    ()
   | arrayCreate     ()
   | assignment      ()
   | ifThenElse      ()
   | ifThen          ()
   | whileClause     ()
   | forClause       ()
   | BREAK           ()
   | letexp          ()
   | parentheses     ()

lvalue : ID          ()
       | lvalue COLON ID ()
       | lvalue LBRACK exp RBRACK ()

sequensing     : LPAREN expsemilist RPAREN ()
expsemilist : expsemilist SEMICOLON exp ()
    	    | exp SEMICOLON exp ()           (* Excl. Parentheses *)

functionCall   : ID LPAREN explistopt RPAREN ()
explistopt : explist ()
	   |         ()  (* empty *)
explist    : explist COMMA exp ()
           | exp               ()

opExpr  : exp DIVIDE exp ()
	| exp TIMES exp ()
	| exp MINUS exp ()
	| exp PLUS  exp ()
	| exp EQ  exp ()
	| exp NEQ exp ()
	| exp GE  exp ()
	| exp GT  exp ()
	| exp LE  exp ()
	| exp LT  exp ()
	| exp AND exp ()
	| exp OR  exp ()
        | MINUS exp %prec UNARYMINUS ()

recordCreate   : ID LBRACE idExpList RBRACE () (* typeid *)
typeid : ID ()
idExpList : recField ()
	  | idExpList COMMA ID recField ()
recField : ID EQ exp ()

arrayCreate    : ID LBRACK exp RBRACK OF exp () (* typeid *)
assignment     : lvalue ASSIGN exp ()
ifThen         : IF exp THEN exp          ()
ifThenElse     : IF exp THEN exp ELSE exp ()
whileClause    : WHILE exp DO exp         ()
forClause      : FOR ID ASSIGN exp TO exp DO exp ()
letexp         : LET decs IN expseq END   ()
expseq : expsemilist ()
       | exp ()
       | ()
parentheses    : LPAREN exp RPAREN        ()

(***** Declaration *****)
decs :               () (* empty *)
     |  decs dec     ()

dec : tydec  ()
    | vardec ()
    | fundec ()

tydec : TYPE typeid EQ ty ()
ty : typeid ()
   | LBRACE tyfields RBRACE ()
   | ARRAY OF typeid ()
tyfields : ()  (* empty *)
	 | tyfieldlist ()
tyfieldlist : tyfieldlist COMMA tyfield ()
	    | tyfield ()
tyfield : ID COLON typeid ()

vardec : VAR ID ASSIGN exp ()
       | VAR ID COLON typeid ASSIGN exp ()

fundec : FUNCTION ID LPAREN tyfields RPAREN EQ exp ()
       | FUNCTION ID LPAREN tyfields RPAREN COLON typeid EQ exp ()

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup Referer   Help   RSS of recent changes
Last-modified: (4849d)