hw1_ans_scan

hw1_ans_scan - COP4020 Spring 2012 Homework Assignment 1...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COP4020 Spring 2012 Homework Assignment 1 (Due Feb. 8 before class). 1. (20 points) Errors in a computer program can be classified according to when they are detected and, if they are detected at compile time, what part of the compiler detects them. Assuming we use C/C++, give an example of each of the following. a) A lexical error, detected by the scanner (hint: the scanner accepts only ASCII characters that are legal to use in C) b) A syntax error, detected by the parser c) A static semantic error, detected by semantic analysis (1) A dynamic semantic error, detected by the code generated by the compiler Answer (5 points each): a) Unterminated quotes, unterminated comments, or invalid characters b) missing semicolons, unmatched parenthesis, etc c) undeclared variables, types mismatches, etc d) C/C++ performs very little dynamic semantic checks. Correct answer would be hard to find. Accept 4/5 for divide by zero, 4/5 for array abound checking. 2. (20 points) Convert the regular expression letter ( letter | digit )* to an NFA using the NFA equivalents (base case, concatenation, alternation, Kleene closure) of the regular expression operations as can be found on page 55 and 56 in the textbook. Answer: Q le‘He r [j clpfis 54' O 0’{ >¥ O lwkr \ p .12 \ J's-t ‘ yore->0 3. (50 points) Consider the following LL(1) grammar for a simplified subset of Lisp P —> E E —> atom —> ’ E ——> ( E Es ) Es —-> E Es —+ s a. Compute the first and follow set of all nonterminals. b. Construct the LL(1) parse table for the grammar c. Show step-by-step (content of stack and input string, as well as the parser action) how the following program is parsed: (cdr ‘ (a b c) ) d. Show the parse tree of the program in c. Answer: (10 points for First, 10 Point for Follow) First(P) = {atom, ’ , (} First (E) = {atom, ’ , (} First (Es) = {atom, ’ , (, e} Follow(P) = {$} Follow(E) = { atom, ’ , (, ) } Follow (Es) = {H b) Parsing table: (10 points, 3 per row) c) (10 points) Stack $E $)Es E ( $)Es E $)Es atom $)Es $)Es E $)Es E ‘ $)Es E $)Es ) Es E ( $)Es ) Es E $)Es ) Es atom $)Es ) Es $)Es ) Es E $)Es ) Es atom $)Es ) Es $)Es ) Es E $)Es ) Es atom $)Es ) Es $)Es ) $)Es $) $ input string (cdr ‘(a b c)) S (cdr ‘(a b c)) 53 (cdr ‘(ab c)) $ cdr ‘(a b 0)) $ ed: ‘(a b c)) $ ‘(ab 0)) $ ‘(a b c)) $ ‘(a b c)) 33 (a b c)) $ (a b c)) $ a b 0)) $ ab c)) $ b c)) $ b 0)) $ b c)) $ c)) S c)) $ c)) $ )) $ )) $ )$ )$ $ action P ——> E E —+ ( E Es ) match ( E ——> atom match atom Es ——> E Es E —> ’ E match ‘ E ——> ( E Es ) match ( E —> atom match atom Es ——> E Es E —> atom match atom Es —> E Es E —-> atom match atom Es —> 8 match ) Es —> 8 match ) accept 4. (10 points) Eliminate left recursion in the following grammar: S —> A a | b A —+ A c I S d | 8 Answer: Intermediate step: S ——> A a | b A——>Ac]Aad|bd| e Finalstep: S—>Aa|b A—+bdA’|A’ A’—>cA’|adA’|£ ...
View Full Document

Page1 / 6

hw1_ans_scan - COP4020 Spring 2012 Homework Assignment 1...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online