COMPILER DEDIGN SET_2

Examination 2010 a. Define CFG, Parse Tree. Ans: CFG: a context free grammar(CFG) is a set of recursive rewriting rules (or productions) used to generate patterns of string. A CFG consists of the following components: A set of terminal symbols, which are the characters of the alphabet that appear in the strings generated by the grammar. Parse tree: a diagrammatic representation of the parsed structure of sentence or string. A parse tree for a grammar G is a tree where• The root is the start symbol for G • The interior nodes are the nonterminal of G • The leaf nodes are the terminal symbol of G • The children of a node T corresponding parse tree, every valid parse tree represents a string generated by the grammar. b. What do you mean by tokens? Describe different type of tokens. Tokens:

AL ANALYSIS

Lexemes are said to be a sequence of characters (alphanumeric) in a token. There are some predefined rules for every lexeme to be identified as a valid token. These rules are defined by grammar rules, by means of a pattern. A pattern explains what a token can be, and these patterns are defined by means of regular expressions. In programming language, keywords, constants, identifiers, strings, numbers, operators, and punctuations symbols can be considered as tokens. For example, in C language, the variable declaration line int value = 100; contains the tokens: int (keyword), value (identifier), = (operator), 100 (constant) and ; (symbol). Type of tokens: C tokens is an individual word present in the C-language. There 6 types1. Keyword: keyword is defined by the software with the specified meaning. In c language 32 keyword are there. 2. Identifiers: user defined variable names. 3. Constructs: the value which is not changed during the execution. 4. Strings: group of characters enclosed in “” 5. Operators: the following are the operators available in c. i. Arithmetic operators. COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

ii. Assignment operator. iii. Logical operator. iv. Increment or decrement operator. v. Bitwise operators; vi. Conditional operator or ternary operator; 6. Special operators: comma (,) to separate; c. How does a lexical analyzer interface between input stream and a parser? Ans: when a lexical analyzer is inserted between the parser and the input stream, it interacts with the two in the manner as shown in following figure; Read character

Pass Token and its attributes Lexical analyzer

input

parser

Push back character

Figure: interface to the lexical analyzer.

It reads the characters from the input groups them into lexemes, and passes the tokens formed by the lexemes; together wit their attribute values, to the later stages of the compiler. d. Define lexemes. What are the properties of lexemes? Ans: a lexical analyzer that reads and converts the input into a stream of tokens to be analyzed by the parser. A sequence of input characters that comprises a single token is called a lexeme. A lexical analyzer can insult a parser from the lexeme representation of tokens. A parse tree pictorially shows how the start symbol of a grammar derivers a string in the language. A parse tree is a tree with the following properties: 1. The root is labeled by the symbols. 2. Each leaf Is labeled by a token or by ∈. 3. Each interior node is labeled by a nonterminal. COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

4. If a is the nonterminal labeling some interior node and X1, X2,……..Xn are the label of the children of that node from left to right, then 𝐴 → 𝑋1 , 𝑋2 … … … … … … … . . 𝑋𝑛

is

a

production.

Here

X1,

X2,……………………………..Xn stand for a symbol that is either a terminal or a nonterminal. as a special case, if 𝐴 →∈ then a node labeled A may have a single child labeled ∈. Examination 2013 a. Define CFG. What are the component of CFG? Ans: context free grammar(CFG): n this section, we will first see the definition of context-free grammar and introduce terminologies used in parsing technology. A context-free grammar has four components: • A set of non-terminals (V). Non-terminals are syntactic variables that denote sets of strings. The non-terminals define sets of strings that help define the language generated by the grammar. • A set of tokens, known as terminal symbols (Σ). Terminals are the basic symbols from which strings are formed. • A set of productions (P). The productions of a grammar specify the manner in which the terminals and non-terminals can be combined to form strings. Each production consists of a nonterminal called the left side of the production, an arrow, and a sequence of tokens and/or on- terminals, called the right side of the production. • One of the non-terminals is designated as the start symbol (S); from where the production begins. The strings are derived from the start symbol by repeatedly replacing a non-terminal (initially the start symbol) by the right side of a production, for that non-terminal. Example We take the problem of palindrome language, which cannot be described by means of Regular Expression. That is, L = { w | w = wR } is not a regular language. But it can be described by means of CFG, as illustrated below: G = ( V, Σ, P, S ) Where: COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

V = { Q, Z, N } Σ = { 0, 1 } P = { Q → Z | Q → N | Q → ℇ | Z → 0Q0 | N → 1Q1 } S={Q} This grammar describes palindrome language, such as: 1001, 11100111, 00100, 1010101, 11111, etc. b. How does a lexical analyzer interface between input stream and a parser? Ans: not found c. Consider the context free grammar 𝑠 → 𝑠𝑠 + |𝑠𝑠 ∗ |𝑥. Show how the string 𝑥𝑥 + 𝑥 ∗ can be generated by this grammar. Ans: the string 𝑥𝑥 + 𝑥 ∗ can be generated by the given grammar𝑠 → 𝑠𝑠 ∗ 𝑠 → 𝑠𝑠 + 𝑠 ∗ 𝑠 → 𝑥𝑠 + 𝑠 ∗ 𝑠 → 𝑥𝑥 + 𝑠 ∗ 𝑠 → 𝑥𝑥 + 𝑥 ∗ d. Construct a DFA for the regular expression (a|b) * abb. First, we construct the NFA for the given expression(𝑎|𝑏) ∗ 𝑎𝑏𝑏 E a E start

0

E

3

2

E

1

6 E 4

b

5

E

7

a

8

b

9

b

E

E Figure (1): NFA for (𝑎|𝑏) ∗ 𝑎𝑏𝑏

COMPILER DEDIGN SET_2

SHAHEEN REZA

10

COMPILER DEDIGN SET_2

Now we construct the DFA from this NFA∈ −𝑐𝑙𝑜𝑠𝑒𝑟(0) → {0,1,2,4,7} = 𝐴 ∈ −𝑐𝑜𝑙𝑜𝑠𝑒𝑟(𝐴, 𝑎) → {3,8} → {1,2,3,4,6,7,8} = 𝐵 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐴, 𝑏) → {5} → {1,2,4,5,6,7} = 𝐶 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐵, 𝑎) → {3,8} = 𝐵 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐵, 𝑏) → {5,9} → {1,2,4,6,7,9} = 𝐷 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐶, 𝑎) → {3,8} = 𝐵 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐶, 𝑏) → {5} = 𝐶 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐷, 𝑎) → {3,8} = 𝐵 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐷, 𝑏) → {5,10} → {1,2,4,5,6,7,10} = 𝐸 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐸, 𝑎) → {3,8} = 𝐵 ∈ −𝑐𝑙𝑜𝑠𝑒𝑟(𝐸, 𝑏) → {5} = 𝐶 𝑡ℎ𝑒 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛 𝑡𝑎𝑏𝑙𝑒 𝑖𝑠 𝑔𝑖𝑣𝑒𝑛 𝑏𝑒𝑙𝑙𝑜𝑤 − State

a

b

A

B

C

B

B

D

C

B

C

D

B

E

E

B

C

The required DFA is given bellow-

COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

a start

a

A

a

b

B a

b

b

D

E

a b

C

b Figure: DFA for (𝑎|𝑏) ∗ 𝑎𝑏𝑏

Examination 2014 a. Discuss the relationship between input, lexical analyzer and parser. Ans: blank b. Define NFA and DFA. Construct a DFA form the following NFA. a a 0

b

b 1

2

3

b

Define NFA and DFA: Construct a DFA from the following NFA First, we construct the transition table for the given NFA: States a b 0(start) 0,1 0 1 − 2 2 − 3 3(terminal) − − COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

now we construct transition table for DFAStates a b {0} (start) {0,1} {0} {0,1} {0,1} {0,2} {0,2} {0,1} {0,3} {0,3} (terminal) {0,1} {0} Now we construct the required DFA for the given NFA-

b start

0

a a {0,1}

b

{0,2}

b

{0,3}

a a b Figure: DFA for the given NFA

c. What is regular expression? Construct a NFA for the following regular expression: (𝑎|𝑏) ∗ 𝑎𝑏𝑏 Ans: follow the question of 2013 figure:(1) d. Define pattern, token and lexeme with example. Pattern: a set of string in the input for which the same of string is described by a rule called a pattern associated with the token. Tokens:

AL ANALYSIS

Lexemes are said to be a sequence of characters (alphanumeric) in a token. There are some predefined rules for every lexeme to be identified as a valid token. These rules are defined by grammar rules, by means of a pattern. A COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

pattern explains what a token can be, and these patterns are defined by means of regular expressions. In programming language, keywords, constants, identifiers, strings, numbers, operators, and punctuations symbols can be considered as tokens. For example, in C language, the variable declaration line int value = 100; contains the tokens: int (keyword), value (identifier), = (operator), 100 (constant) and; (symbol). Lexeme: a lexical analyzer that reads and converts the input into a stream of tokens to be analyzed by the parse. A sequence of input characters that comprises a single token is called a lexeme. A lexical analyzer can insulate a parser from the lexeme representation of tokens. Examination 2012 a. What are the difference between DFA and NFA? Differences between DFA and NFA: DFA NFA 1. The term DFA stands for 1. The term NFA stands for non deterministic finite automata. deterministic finite automata. 2. In DFA we cannot have ∈ 2. In NFA we may have ∈ transition. transition. 3. In this case in the transition 3. In this case in the transition table each entry is a single table each entry is a set even if stately. the set is a singleton. 4. In this case. On each input there 4. In this case on each input, the is one and only one state to automaton can transition from which the automation can its current state to several transition from its current states at once. state. 5. In this case 𝛿 is a function that 5. In this case 𝛿 is a function that takes a state and output symbol takes a state and input symbol as arguments. But returns as arguments but returns a set exactly one state. of zero, one or more state. b. Write the algorithm to minimize the number of state of a DFA. Ans: algorithm: Minimizing the number of states of a DFA. Input: A DFA D with set of states’ S, input alphabetΣ, state state s0, and set of accepting states F. Output: A DFA D’ accepting the same language as D and having as few state as possible. COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

Method: 1. Start with an initial partition 𝜋 with two program, F and S-F, the accepting and nonaccepting states of D. 2. Apply the procedure of Figure: (2) to construct a new partition 𝜋𝑛𝑒𝑤 . initially, let 𝜋𝑛𝑒𝑤 =𝜋; for (each group G of 𝜋) { Partitioning G into subgroups such that two state s and t a are in the same subgroup if and only if for all input symbols a, states’ s and t have transitions on a to states in the same group of 𝜋; /*at worst, a state will be in a subgroup by itself */ Replace G in 𝜋𝑛𝑒𝑤 by the set of all subgroups formeds; } Figure (2): construction of 𝜋𝑛𝑒𝑤 3. If 𝜋𝑛𝑒𝑤 = 𝜋, let 𝜋𝑛𝑒𝑤 = 𝜋 and continue with step (4). Otherwise, repeat step (2) with 𝜋𝑛𝑒𝑤 in place of 𝜋. 4. Choose one state in each group of 𝜋𝑛𝑒𝑤 𝑎𝑠 the representative for that group. The representative will be the states of the minimum state DFA 𝐷′. The other components of 𝐷′ are constructed as follow: a. The state state of 𝐷′ is the representative of the group containing the start state of 𝐷′ . b. The accepting states of 𝐷′ are the representatives of those groups that contain an accepting state of 𝐷′ . Note that each group contains either only accepting states, or only nonaccepting states, because we started by separating those two classes of states, and the procedure of fig: 2 always forms new groups that are subgroups of previously constructed groups. c. Let s be the representative of some group G of 𝜋𝑓𝑖𝑛𝑎𝑙 , and let the transition of D from s on input a be to state t. let r be the representative of t’s group H. then in 𝐷′ , there is a transition from s to r on input a. note that in D, every state in group G must go to some state of group H on input a, else, group G would have been split accepting to fig.(2). c. What are the main functions of lexical analyzer? List the various error recovery strategies in lexical analysis. Ans: d. Define regular expression with example. what are the rules of constructing regular expression? Examination 2015 a. Define CFG, pars tree, and syntax tree. COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

b. What is ambiguity? Explain with example. Ambiguity: ambiguity is a type of uncertainty of meaning in which several interpretations are possible. A grammar can have more than one parse tree generating a given string of tokens such a grammar is said to be ambiguous. To show that a grammar is ambiguous, all we need to do is find a token string that has more than one parse tree since a string with more than one parse tree usually has more than one meaning for compiling applications we need to design ambiguous grammars, or to use ambiguous grammars with additional rules to resolve. The ambiguities.

String

string _

+ String

string String

string

9 +

_ String

String

2

string

string 2

5 9

5 Fig: two parse trees for 9-5+2

c. Write down the input buffering technique with code. Ans: input buffer technique: • Data transfer by an input channel is stored in the buffer. • After the data transfer the processor access the data from the buffer. COMPILER DEDIGN SET_2

SHAHEEN REZA

COMPILER DEDIGN SET_2

Code: #𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ > 𝑣𝑜𝑖𝑑 𝑚𝑎𝑖𝑛( ) { 𝑐ℎ𝑎𝑟 𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑[7] = ”𝑠𝑒𝑐𝑟𝑒𝑡”; 𝑐ℎ𝑎𝑟 𝑔𝑢𝑒𝑠𝑠[7]; 𝑐ℎ𝑎𝑡 𝑐𝑜𝑛𝑡 =′ 𝑌 ′ ; 𝑖𝑛𝑡 𝑐𝑜𝑢𝑛𝑡 = 0; 𝑤ℎ𝑖𝑙𝑒((𝑐𝑜𝑛𝑡 ==′ 𝑌 ′ | |𝑐𝑜𝑛𝑡 ==′ 𝑦 ′ )&&(𝑐𝑜𝑢𝑛𝑡 < 6)) { 𝑝𝑟𝑖𝑛𝑡𝑓(enter password character#%d:, 𝑐𝑜𝑢𝑛𝑡 + 1); 𝑔𝑢𝑒𝑠𝑠[𝑐𝑜𝑢𝑛𝑡] = 𝑔𝑒𝑡𝑐ℎ𝑎𝑟(); 𝑖𝑓(𝑔𝑢𝑒𝑠𝑠 [𝑐𝑜𝑢𝑛𝑡] == 𝑝𝑎𝑠𝑤𝑜𝑟𝑑 [𝑐𝑜𝑢𝑛𝑡]) { 𝑝𝑟𝑖𝑛𝑡𝑓(“𝑌𝑜𝑢 𝑔𝑢𝑒𝑠𝑠𝑒𝑑 𝑐𝑜𝑟𝑟𝑒𝑐𝑡𝑙𝑦! ”); 𝑐𝑜𝑢𝑛𝑡 + +; } 𝑒𝑙𝑠𝑒 𝑝𝑟𝑖𝑛𝑡𝑓(“𝑌𝑜𝑢 𝑑𝑖𝑑 𝑛𝑜𝑡 𝑔𝑢𝑒𝑠𝑠 𝑐𝑜𝑟𝑟𝑒𝑐𝑡𝑙𝑦! ”); 𝑖𝑓(𝑐𝑜𝑢𝑛𝑡 < 6) { 𝑝𝑟𝑖𝑛𝑡𝑓(“\𝑛 𝑑𝑜 𝑦𝑜𝑢 𝑤𝑎𝑛𝑡 𝑡𝑜 𝑐𝑜𝑛𝑡𝑖𝑛𝑢𝑒? (𝑒𝑛𝑡𝑒𝑟 𝑢𝑝𝑝𝑒𝑟𝑐𝑎𝑠𝑒𝑠/ 𝑙𝑜𝑤𝑒𝑟 𝑐𝑎𝑠𝑒 𝑌 𝑓𝑜𝑟 𝑦𝑒𝑠, 𝑒𝑛𝑡𝑒𝑟 𝑎𝑛𝑦 𝑜𝑡ℎ𝑒𝑟 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟 𝑓𝑜𝑟 𝑁𝑂): ”); 𝑐𝑜𝑢𝑛𝑡 = 𝑔𝑒𝑡𝑐ℎ𝑎𝑟(); } 𝑒𝑙𝑠𝑒 𝑝𝑟𝑖𝑛𝑡𝑓("𝑦𝑜𝑢 𝑎𝑟𝑒 𝑑𝑜𝑛𝑒! "); } } d. What is a predictive parser? Explain the model of a predictive parser.

COMPILER DEDIGN SET_2

SHAHEEN REZA

compiler design__2.pdf

Page 1 of 11. COMPILER DEDIGN SET_2 SHAHEEN REZA. COMPILER DEDIGN SET_2. Examination 2010. a. Define CFG, Parse Tree. Ans: CFG: a context ...

375KB Sizes 1 Downloads 302 Views

Recommend Documents

COMPILER DESIGN.pdf
b) Explain the various strategies used for register allocation and assignment. 10. 8. Write short notes on : i) Error recovery in LR parsers. ii) Loops in flow graphs.

Compiler design.pdf
c) Briefly explain main issues in code generation. 6. ———————. Whoops! There was a problem loading this page. Compiler design.pdf. Compiler design.pdf.

ClamAV Bytecode Compiler - GitHub
Clam AntiVirus is free software; you can redistribute it and/or modify it under the terms of the GNU ... A minimalistic release build requires 100M of disk space. ... $PREFIX/docs/clamav/clambc-user.pdf. 3 ...... re2c is in the public domain.

Compiler design.pdf
3. a) Consider the following grammar. E → E + T T. T → T *F F. F → (E) id. Construct SLR parsing table for this grammar. 10. b) Construct the SLR parsing table ...

compiler design_1.pdf
It uses the hierarchical structure determined by the. syntax-analysis phase to identify the operators and operands of. expressions and statements. Page 1 of 7 ...

CS6612-COMPILER-LABORATORY- By EasyEngineering.net.pdf ...
1. Implementation of symbol table. 2. Develop a lexical analyzer to recognize a few patterns in c (ex. Identifers, constants,. comments, operators etc.) 3. Implementation of lexical analyzer using lex tool. 4. Generate yacc specification for a few sy

Compiler Design Syllabus.pdf
software design(PO→BCG ). iv. Working skills in theory and application of finite state machines, recursive descent,. production rules, parsing, and language ...

CSE401 Introduction to Compiler Construction
intrinsicState. ConcreteFlyweight. Operation(extrinsicState). allState. UnsharedConcreteFlyweight. CSE403 Sp10. 10. Participants. • Flyweight (glyph in text example). – Interface through which flyweights can receive and act on extrinsic state. â€

Emscripten: An LLVM-to-JavaScript Compiler - GitHub
May 14, 2013 - Emscripten, or (2) Compile a language's entire runtime into ...... html.) • Poppler and FreeType: Poppler12 is an open source. PDF rendering ...

Teaching Compiler Development to Undergraduates ...
large software systems. In this paper, we ... The C- programming language is basically a small trac- ..... on the C- file name and all the C- code is translated to.

interpreter vs compiler pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. interpreter vs ...

Basics of Compiler Design
So why do most computer science institutions offer compiler courses and often make ... programmers and computer scientists. ..... consists entirely of as and bs.

Test Suite Compiler (TSC)
Mar 27, 2016 - redirects stdout to a string buffer. After. sizeOfBuffer bytes printed, the test is aborted with a FATAL failure. int _printFault(const char* limit,.

Benchmarking the Compiler Vectorization for Multimedia Applications
efficient way to exploit the data parallelism hidden in ap- plications. ... The encoder also employs intra-frame analysis when cost effective. ... bigger set of data.

Emscripten: An LLVM-to-JavaScript Compiler - GitHub
Apr 6, 2011 - written in languages other than JavaScript on the web: (1). Compile code ... pile that into JavaScript using Emscripten, or (2) Compile a ... detail the methods used in Emscripten to deal with those ..... All the tests were run on a Len

Concurrency-aware compiler optimizations for hardware description ...
semantics, we extend the data flow analysis framework to concurrent threads. .... duce two auxiliary concepts—Event Vector and Sensitivity Vector—in section 6, ...

Improving Compiler Heuristics with Machine Learning
uses machine-learning techniques to automatically search the space ..... We employ depth-fair crossover, which equally weighs each level of the tree [12]. ...... Code to Irregular DSPs with the Retargetable,. Optimizing Compiler COGEN(T). In Internat

principles of compiler design pdf
Download. Connect more apps... Try one of the apps below to open or edit this item. principles of compiler design pdf. principles of compiler design pdf. Open.

Guest lecture for Compiler Construction, Spring 2015
references and (user-defined) exceptions. ✓ modules, signatures, abstract types. The CakeML language. = Standard ML without I/O or functors. The verified machine-code implementation: parsing, type inference, compilation, garbage collection, bignums

bottom up parsing in compiler design pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. bottom up ...