Please use the VCL image "Linux Lab Machine (Realm RHEnterprise 6)" to solve the programming exercises. (You may use your own laptop or any other environment but you will be graded against this VCL image.)
Turn in files calc.l and calc.y.
Turn in files scan.c and scan.pdf.
Generate a scanner and parser for the parpseudo language below. Of course, once you generate a compiler, it is no longer a pseudo language, strictly speaking :-) Consider the following notation for pseudo code:
| Program | Block |
| Block | VAR Declarations BEGIN ... END |
| Test | IF Comparison THEN ... ELSE ... ENDIF |
| IF Comparison THEN ... ENDIF | |
| Loop | WHILE Comparison DO ... ENDWHILE |
| REPEAT ... UNTIL Comparison ENDREPEAT | |
| FOR Variable := Expression TO Expression DO ... ENDFOR | |
| PARFOR Variable := Expression TO Expression PRIVATE Variables REDUCE ReduceOp Variables DO ... ENDPARFOR | |
| Procedure | PROC Procname ( Parameters ) Block ENDPROC |
| PARPROC Procname ( Parameters ) Block ENDPARPROC | |
| Parameter | Passby Variable : Type |
| Passby | IN | OUT | INOUT |
| Input | READ(Variable) |
| Output | WRITE(Variable) |
| Declaration | Variable : Type |
| Type | INT | REAL | INT[Expression] | REAL[Expression] |
| Assignment | Variable := Expression |
| Variable[Expression] := Expression | |
| Expression | Variable |
| Variable[Expression] | |
| IntegerNumber | |
| RealNumber | |
| Expression ArithmeticOp Expression | |
| INT ( Expression ) | |
| Sign Expression | |
| LogicOp | AND OR |
| ComparisonOp | = <> > < >= <= |
| Comparison | Expression ComparisonOp Expression |
| Comparison LogicOp Comparison | |
| NOT Comparison |
To make your life easier, here is the grammar of the pseudo language in EBNF notation (Notice that [ X ] means X is optional, but '[' and ']' refer to the open/close bracket symbols.):
Program ::= Block
Block ::= [ Declarations ] BEGIN { Statement } END
Declarations ::= VAR { Varlist : Type ; }
Varlist ::= Variable { , Variable }
Type ::= BasicType | ArrayType
ArrayType ::= BasicType '[' Expression ']'
BasicType ::= INT | REAL
Statement ::= Assignment ; | Block ; | Test ; | Loop ; | Input ; | Output ; | Procedure ; | ProcCall ;
Assignment ::= Variable := Expression | Variable'['Expression']' := Expression ;
Expression ::= Variable | Variable'['Expression']' | [ Sign ] IntegerNumber |
[ Sign ] RealNumber | Expression ArithmeticOp Expression | INT '(' Expression ')' | Sign Expression
ArithmeticOp ::= + | - | * | / | DIV | MOD
LogicOp ::= AND | OR
ComparisonOp ::= = | <> | < | > | <= | >=
Comparison ::= Expression ComparisonOp Expression |
Comparison LogicOp Comparison | NOT Comparison
Test ::= IF Comparison THEN { Statement } [ ELSE { Statement } ] ENDIF
Loop ::= WHILE Comparison DO { Statement } ENDWHILE |
REPEAT { Statement } UNTIL Comparison ENDREPEAT |
FOR Variable := Expression TO Expression DO { Statement } ENDFOR
PARFOR Variable := Expression TO Expression ParMod DO { Statement } ENDPARFOR
ParMod ::= [ PRIVATE Varlist ] { REDUCE ReduceOp Varlist }
ReduceOp ::= + | - | MIN | MAX
Input ::= READ '(' Variable ')'
Output ::= WRITE '(' Expression ')'
Procedure ::= PROC Procname '(' [ Parameters ] ')' Block ENDPROC |
PARPROC Procname '(' [ Parameters ] ')' Block ENDPARPROC
Parameters ::= Parameter { , Parameter }
Parameter ::= Passby Variable : Type
Passby ::= IN | OUT | INOUT | REF
ProcCall ::= Variable '(' Arguments ')'
Arguments ::= Expression { , Expression }
Turn in files pseudo.l and pseudo.y.
Hints:
Other useful references: