CS445

This is a sample of questions from earlier midterms.

One of the problems below is extra credit that applies to this test.


  1. (10 pts) What is the difference between a top down parser and a bottom up parser? Be specific. Name one top down and one bottome up parser type.

  2. (10 pts) Name the six parts of a compiler on page 7 in your book and the products of each part.

  3. (15 pts) Give an example of a
    1. lexical analysis error.

    2. syntactic analysis error.

    3. semantic analysis error.

  4. (10 pts) Why don't we use regular expressions for both tokenizing and for parsing? Be specific and complete.

  5. Write a regular expression suitable for flex for the following or tell me why it cannot be done.

    1. (20 pts)An integer which does not begin with a 0 and an OPTIONAL sign of + or - also it must match just a bare 0. For example the pattern will match: 42, +1, -1, 0, 3000, -101 but not 007, ++8, -0101

    2. (10 pts)A list of lowercase letters separated by commas. (e.g. x,y,x)

    3. (10 pts)A number of a's followed by an equal number of b's. For example: ab, aaabbb, aaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbb but not aab.

  6. Consider this grammar:

    stmt -> IF ( test ) stmt ELSE stmt
    stmt -> ACTION 
    stmt -> compound-stmt
    compound-stmt -> { stmt-list }
    stmt-list -> stmt | stmt ; stmt-list
    test -> EXP
    
    and consider the statement IF ( EXP ) { ACTION ; ACTION } ELSE ACTION
    1. (10 pts) Draw the parse tree for this statement.

    2. (10 pts) Draw the syntax tree for this statement.

    3. (20 pts) Draw the treeNode data structure from our C- compiler for this statement.

  7. (20 pts) In class we discussed at great length the trick for solving the dangling else problem that involved splitting the if statement into two classes of if statements one called matched and the other called unmatched. Write out the grammar for the matched/unmatched solution from class for the C- if statement using Bison style formating. Do not do the while statement. Do not include the action part of the grammar. Begin with the start symbol statement and assume the only other statements are return statments which look like RETURN exp ; and that you have an nonterm exp you can use in the test position of the if.

  8. (20 pts extra credit) Add the loop forever statement which looks like this: loop statement forever and the while ( exp ) statement statement.

  9. (10 pts) In Perl you can have two kinds of if statements. The first is a postfix if that works on the statement preceding it:

    statement if exp ;

    and the second is the regular if but it requires that the braces be used without exception:

    if ( exp ) { statement }.

    Why must the second kind of if statement require the braces? Said another way: give an example and explain where not having the braces would be a problem.

  10. (10 pts) What is a shift/reduce error? Give an example grammar where this is the error.

  11. (10 pts) What is a reduce/reduce error? Give an example grammar where this is the error.

  12. (10 pts) What is a recursive descent parser?

  13. (10 pts) Show how left recursion removal is done for exp -> exp + term | term

  14. (10 pts) If the state machine for an LL or LR grammar arrives at an empty cell in the parse table what does that mean? Be specific.

  15. (30 pts)

    In the mock grammar below what are the first and follow sets? Note: this grammar is slightly different than in the practice test.

    %token IF EXP ELSE RR ENDIF
    %%
    statement : IF '(' EXP ')' statement ENDIF
              | statement IF '(' EXP ')'
              | return_stmt
              | compount_stmt
              ;
    

    return_stmt : RR ;

    compount_stmt : '{' statementlist '}' | '[' statementlist ']' ;

    statementlist : statement | statementlist ';' statement

  16. (40 pts) Given the following LL(0) parse table:

    Be sure to study how to do LL(0) parsing using a parse table. This question is much like the next but for a top down parser. Use examples from the book.

  17. (40 pts) Given the following SLR(1) parse table:

    -()$S
    0s2r(S->empty)r(S->empty)1
    1--accept-
    2s2r(S->empty)r(S->empty)3
    3-s4--
    4s2r(S->empty)r(S->empty)5
    5-r(S->(S)S)r(S->(S)S)-

    Show the parsing of the input (( )( )) by making a table of parse stack input and action

  18. (10 pts) Number steps below in order that you would take creating an SLR parser:
    __ create NFA
    __ build a grammar for the language
    __ create DFA
    __ build parse table
    __ create LR(0) items

  19. (10 pts) What is the main difference between an SLR(1) and an LR(1) parser?


Robert Heckendorn Up One Level Last updated: Mar 11, 2004 11:21