0% found this document useful (0 votes)
32 views4 pages

Midterm2 Spring2021 AnthonyLe

This document contains a 7 question midterm exam for CS 323. It covers topics like formal grammar definitions, Chomsky's hierarchy of formal languages, parsing techniques like LL parsing and recursive descent parsing. Students are asked to define grammars, identify ambiguities, construct parsing tables, write recursive descent parsers, and parse strings based on given grammars.

Uploaded by

John Johnson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Midterm2 Spring2021 AnthonyLe

This document contains a 7 question midterm exam for CS 323. It covers topics like formal grammar definitions, Chomsky's hierarchy of formal languages, parsing techniques like LL parsing and recursive descent parsing. Students are asked to define grammars, identify ambiguities, construct parsing tables, write recursive descent parsers, and parse strings based on given grammars.

Uploaded by

John Johnson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS 323 – Midterm 2 – 200 points – 7 Questions

Section ______ Name Jacob Hajjar

NUMBER AND ANSWER ALL THE QUESTIONS (20-30 points each)


SHOW YOUR WORK AND ADD MORE PAGES AS NEEDED
Technically CLOSED BOOK – NO COPY & PASTE

1. Use your own words to answer the following questions (30):

a) What is the formal definition of grammar G (explain the tuples)? (10)


Grammar is how we recognize the complete set of patterns defined by the rules
where T are the terminal symbols or real symbols that can be used in the language, N are
the nonterminal symbols which can represent various terminal symbols, S is the starting
symbol, and R are the rules or sets of patterns of terminals and nonterminals recognized
by the grammar.
b) Explain the different types of languages and complexity level in Chomsky’s hierarchy.
(20)
Chomsky’s hierarchy has 4 levels of languages from 0 to 3 which go from the
most complex to the simplest. There are type 0 languages or unrestricted languages which
can have more than one non-terminal on the left hand side and both terminals and
nonterminals on both sides; these can only be solved by a Turing machine. There are type
1 languages called context sensitive languages which have less nonterminals on the left
hand side and can remember more states than level 0 languages, type 2 languages or
context-free which have only a single non-terminal on the left hand side and on the right
can have more than one terminal and non-terminal, these can be solved by push down
automaton. Then there are type 3 or regular languages which are the simplest and have
only one nonterminal on the left side, and at most one nonterminal on the right.

2. Given the following productions, and the string w = ‘abab’. Is this grammar ambiguous
and if so, explain ambiguity and show why? (30)

1) E −> aEbE 2) E −> bEaE 3) E −> cdE 4) E −> ε


Ambiguity is when two different parse trees can be used to accept the same string
Since both these parse trees accept the same string w, this grammar is ambiguous.

3. Remove all left recursions and backtracking in the following productions if needed. (30)

1)E −> EaE 2)E−> F 3)F−> mE 4)F−> Tde 5) F −> Tfg 6) T −> id
E->FE’ | F
E’-> aEE’ | ε
F-> mE | TF’
F’ -> de | fg
T-> id

4. Define the First and Follow sets for each non-terminal symbol (30)

D −> ONE
O −> xy | b | ε
N −> Ez | cd | ε
E −> m | fg | ε

First(O) -> {xy, b, ε}


First(N) -> {m, fg, cd, ε}
First(E) -> {m, fg, ε}
First(D) -> {xy, b, m, fg, cd, ε}
Follow(O) -> {m, fg, z, cd}
Follow(N) -> {m, fg}
Follow(E) -> {m, fg}
Follow(D) -> {$}

5. Given the following productions, construct the parsing table for table driven predictive
parser - it is a top-down parser. (30)
1) S −> S & C 2) S −> S @ C 3) S −> C 4) C −> x

S-> CS’
S’-> &CS’ | @CS’ | ε
C->x

{0, &, @, x, $},


{S, 0, 0, CS’, 0},
{S’, &CS’, @CS’, 0, ε},
{C, 0, 0, x, 0};

6. Given the following production rules, write a recursive descent function that returns a
boolean value for the productions Q. Write the function using syntactically correct C, C++,
C#, python (for 20 points maximum) or pseudo code (15 points). Use any of the pre-existing
functions lexer(), getNextChar(), currentChar(), first(), follow(), token(), backup(), error()
or match() only if needed. (20 points total)

1) E −> TQ 2) Q −> #TQ 3) Q −> ε 4) Τ −> id

bool parser(vector<string> tokens) {


stack <string> qStack;
qStack.push(“$”);
qStack.push(“E”);
tokens.push_back(“$”);
int i;

while (true){
if (tokens[i] == qStack.top()) {
i++; //the lexer iteration
qStack.pop();
}
else if (qStack.top == “$”){
return true;
}
else if (qStack.top == “E”) {
qStack.pop();
qStack.push(Q);
qStack.push(T);
}
else if (qStack.top == “Q”) {
qStack.pop();
if (tokens[i] == “#”) {
qStack.push(Q);
qStack.push(T);
qStack.push(#);
}
else {
//epsilon, do nothing
}
}
else if (qStack.top == “T”) {
qStack.pop();
if (type(tokens[i]) == id) {
qStack.push(tokens[i]);
}
else {
return false;
}
}
else {
return false;
}
}
}

7. Given the following predictive parsing table, parse the string “ {xyx} ”
to see if it accepted. (30 points)

x y { } $

S CB CB
B yCB ε ε
C x {S}

Use any of the following: Stack input Production/Action


$S {xyx}$ pop(S), push(B, C)
$BC {xyx}$ pop(C), push(}, S, {)
$B}S{ {xyx}$ pop({), lexer();
$B}S xyx}$ pop(S), push(B, C)
$B}BC xyx}$ pop(C), push(x)
$B}Bx xyx}$ pop(x), lexer();
$B}B yx}$ pop(B), push(B, C, y)
$B}BCy yx}$ pop(y), lexer();
$B}BC x}$ pop(C), push(x)
$B}Bx x}$ pop(x), lexer();
$B}B }$ pop(B), push(ε)
$B} }$ pop(}), lexer();
$B $ pop(B), push(ε)
$ $ stack empty

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy