Homework 1

Deadline: see web page
Assignments: All parts are to be solved individually (turned in electronically, written parts in ASCII text, NO Word, postscript, etc. permitted unless explicitly stated).

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.)

  1. (5 points) Extend the simple calculator on the last slide of lec1.ppt to support both multiplication/division and addition/subtraction, yet on decimal numbers in "dot" notation an arbitrary leading/trailing number of digits such as 2., .2, 0.2, 2.0, 2 etc.

    Turn in files calc.l and calc.y.

  2. (10 points) Construct a DFA for the scanner of the last excercise by hand and turn in its graphics representation as a PDF. Then code it up manually translating the state machine one-to-one into C code with gotos. Make it compatible with the parser (calc.y) from the previous exercise, and test it out.

    Turn in files scan.c and scan.pdf.


  3. 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:

    ProgramBlock
    BlockVAR Declarations BEGIN ... END
    TestIF Comparison THEN ... ELSE ... ENDIF
    IF Comparison THEN ... ENDIF
    LoopWHILE 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
    ProcedurePROC Procname ( Parameters ) Block ENDPROC
    PARPROC Procname ( Parameters ) Block ENDPARPROC
    ParameterPassby Variable : Type
    PassbyIN | OUT | INOUT
    InputREAD(Variable)
    OutputWRITE(Variable)
    DeclarationVariable : Type
    Type INT | REAL | INT[Expression] | REAL[Expression]
    AssignmentVariable := Expression
    Variable[Expression] := Expression
    ExpressionVariable
    Variable[Expression]
    IntegerNumber
    RealNumber
    Expression ArithmeticOp Expression
    INT ( Expression )
    Sign Expression
    LogicOpAND OR
    ComparisonOp= <> > < >= <=
    ComparisonExpression ComparisonOp Expression
    Comparison LogicOp Comparison
    NOT Comparison
    Notice:

    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 }
    

  4. (30 points) Specify the lexical analysis starting with pseudo.l (incomplete) and a Makefile so that you can use flex to generate a scanner. This includes recognition of operators, key words, numbers (int and real, e.g., 123 and 12.3 but no exponent notation), identifiers and handling of white space. The scanner should generate exactly those tokens (declared as constants in the parser specification, see below) given by the syntax above. Allow for a reference to the line number (later used by the parser). Identifiers and numbers should be provided in yylval as str_ptr / int_val / double_val.
  5. (55 points) Specify the syntax analysis starting with pseudo.y (incomplete) so that you can use bison to generate a parser. This includes declaration of all tokens (with type information for identifiers and numbers), associativity and precedence of operators, grammar rules as indicated by the EBNF above (only minor changes are necessary for the transformation to rules) as well as syntax errors indicated with a line number and the affected token.
  6. Files for testing: error.psd and ok.psd.

    Turn in files pseudo.l and pseudo.y.

    Hints:

    Other useful references: