CS 433 - Homework 3 - 2/25/08

Parsing with Yacc; Context-free grammars

Due: Monday, 3/3/08, at 2pm

Task 1

Create a parser for our Omg language using the scanner generator "Yacc". Start by copying my directory ~schar/cs433/hw3, and add your symbol table implementation from homework 1, and your scanner from homework 2:

    ~> cd cs433
    ~/cs433> cp -r ~schar/cs433/hw3 .
    ~/cs433> cp hw2/symtab.c hw3
    ~/cs433> cp hw2/symtab.h hw3
    ~/cs433> cp hw2/scanner.l hw3
    ~/cs433> ls hw3
    listp   Makefile  printtree.c  symtab.c  syntax.h   syntree.h
    main.c  parser.y  scanner.l    symtab.h  syntree.c
Next, change "scanner.l" as reflected by the output of "diff" below:
~/cs433> diff hw2/scanner.l hw3/scanner.l
5c5
<  * solutions for homework 2 by ...
---
>  * solutions for homework 2 by ... - modified for hw 3
16c16
< #include "tokens.h"
---
> #include "y.tab.h"  /* NEW - was tokens.h */
20c20
< YYSTYPE yylval;
---
> extern YYSTYPE yylval;  /* NEW - now extern since defined in parser.c */
Your task is to turn the file "parser.y" into a valid Yacc input file for the Omg language, as specified by the (hopefully final ;) "official" Omg Grammar, and explained in class. I'm also including a useful script called "listp" that prints a file with line numbers.

When you're done, your program should work as follows:

~/cs433/hw3> ./listp ~schar/cs433/omg/testhw3.omg
1 # testhw3.omg - test program for hw 3
2 glob num[1..3] a;  # global array
3
4 p() {
5     a[1] := 2 * 3 + 4;
6 }
7
8 omg() {
9     if yes,
10         if 3 <> 4,
11             p();
12         else
13             a[2] := 5 mod 2;
14 }

~/cs433/hw3> ./omg -t ~schar/cs433/omg/testhw3.omg
#line   2: program (2)
#line   2:    globlist
#line   2:       vardecl (2)
#line   2:          arraytype (3)
#line   2:             type = num
#line   2:             const = 1
#line   2:             const = 3
#line   2:          decllist
#line   2:             decl (2)
#line   2:                ident = a
#                         NULL node
#line   4:    funlist
#line   4:       fundecl (4)
#                   NULL node
#line   4:          ident = p
#                   NULL node
#line   4:          body (1)
#line   5:             stmtlist
#line   5:                assign (2)
#line   5:                   array (2)
#line   5:                      ident = a
#line   5:                      const = 1
#line   5:                   plus (2)
#line   5:                      times (2)
#line   5:                         const = 2
#line   5:                         const = 3
#line   5:                      const = 4
#line   8:       fundecl (4)
#                   NULL node
#line   8:          ident = omg
#                   NULL node
#line   8:          body (1)
#line   9:             stmtlist
#line   9:                if (3)
#line   9:                   const = 1
#line  10:                   if (3)
#line  10:                      ne (2)
#line  10:                         const = 3
#line  10:                         const = 4
#line  11:                      funcall (2)
#line  11:                         ident = p
#                                  NULL node
#line  13:                      assign (2)
#line  13:                         array (2)
#line  13:                            ident = a
#line  13:                            const = 2
#line  13:                         mod (2)
#line  13:                            const = 5
#line  13:                            const = 2
#                            NULL node
Test your parser on the files in "~schar/cs433/omg/", and try debugging some of the files in the hw2 subdirectory. In particular, fix your own programs submitted last week so that they parse and resubmit them by adding them to the OMG line in the Makefile. (Please make sure to have your name in a comment in your omg files, in addition to using your initials as a prefix in the filenames.)


Task 2

Solve the following written problems about Context Free Grammars:

  1. Let G be defined by:
        S  ->  A B
        A  ->  A a  |  b B
        B  ->  a    |  S b
    
    (a) Give a leftmost and a rightmost derivation of the string "baabaab" in the grammar G.

    (b) Show a parse tree for "baabaab".

  2. (Part of problem 4.2.2 from our textbook) What language is generated by the grammar:
        S  ->  a S b S | b S a S | e
    
    Give an informal description of the language generated, and justify your answer.

  3. Show that the grammar
        S  ->  ( A )  |  b
        A  ->  S      |  A ; A
    
    is ambiguous and construct an unambiguous grammar for the same language. Include an informal description of the language generated by the grammar.

  4. A production of the form A -> A a  is said to be left-recursive. Similarly, a production of the form B -> bB  is said to be right-recursive. Show that any grammar that contains both left and right recursive productions with the same left-hand-side symbol must be ambiguous.


What to hand in

Hand in printouts of your completed file parser.y and your corrected Omg programs, and your written answers to the grammar problems. Also submit your programs electronically by typing "make submit".

Please note on your submission how much time it took you.