You are on page 1of 6

7.

)Write brief note with properties on alphabet, string, language, regular expression, grammar
An alphabet is any finite set of symbols. Typical examples of symbols are letters, digits, and punctuation. The set {0,1) is the binary alphabet. A string over an alphabet is a finite sequence of symbols drawn from that alphabet. In language theory, the terms "sentence" and "word" are often used as synonyms for "string." A language is any countable set of strings over some fixed alphabet. Abstract languages like 0, the empty set, or (), the set containing only the empty string, are languages under this definition. The definition of "language" does not require that any meaning be ascribed to the strings in the language.

Regular expressions is a notation that has come into common use for describing all the languages that can be
built from the operators that can be applied to the symbols of some alphabet. The regular expressions are built recursively out of smaller regular expressions, using the rules described below. Each regular expression r denotes a language L(r), which is also defined recursively from the languages denoted by r's subexpressions. A grammar describes the hierarchical structure of programs. It is defined in terms of elementary symbols called terminals and variable symbols called nonterminals. These symbols represent language constructs. The rules or productions of a grammar consist of a nonterminal called the head or left side of a production and a sequence of terminals and nonterminals called the body or right side of the production

12.)What are the differences between NFA and DFA?


1.DFA stands for Deterministic Finite Automata while NFA stands for Nondeterministic Finite Automata. 2.Both are transition functions of automata. In DFA the next possible state is distinctly set while in NFA each pair of state and input symbol can have many possible next states. 3.NFA can use empty string transition while DFA cannot use empty string transition. 4.NFA is easier to construct while it is more difficult to construct DFA. 5.Backtracking is allowed in DFA while in NFA it may or may not be allowed. 6.DFA requires more space while NFA requires less space. 7.While DFA can be understood as one machine and a DFA machine can be constructed for every input and output, 8.NFA can be understood as several little machines that compute together, and there is no possibility of constructing an NFA machine for every input and output.

37.)What do you mean by Left Factoring? How do you remove Left Factoring? What is the need to remove Left Factoring? Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive, or top-down, parsing. When the choice between two alternative A-productions is not clear, we may be able to rewrite the productions to defer the decision until enough of the input has been seen that we can make the right choice. For example, if we have the two productions stmt -> if expr then stmt else stmt | if expr then stmt

on seeing the input if, we cannot immediately tell which production to choose to expand stmt. In general, if A -> l I 2 are two A-productions, and the input begins with a nonempty string derived from , we do not know whether to expand A to 1 or 2. However, we may defer the decision by expanding A to A'. Then, after seeing the input derived from , we expand A' to 1 or to 2. That is, left-factored, the original productions become:A->A A->1 | 2 METHOD: For each nonterminal A, find the longest prefix common to two or more of its alternatives. If a! = i.e., there is a nontrivial common prefix - replace all of the A- productions A ->1 | 2 | |n | , where y represents all alternatives that do not begin with , by A->A | A->1 | 2 |3..|n Here A' is a new nonterminal. Repeatedly apply this transformation until no two alternatives for a nonterminal have a common prefix. If we do not left factor the grammar will remain ambiguous. And on a particular input which rule to be chosen cannot be decided clearly which becomes especially difficult in case of a predictive parser. 42.)Write an algorithm to Construct LL(1) Parsing Table? Input: Grammar G Output: Predictive parsing table or LL(1) table M Method: For each production A - > of the grammar do the following:For each terminal a in FIRST() add A-> to M[A,a]. If is in FIRST() then for each terminal b on FOLLOW(A) add A-> to M[A,b]. If is in FIRST() and $ is in FOLLOW(A) then add A-> to M[A,$]. If, after performing the above, there is no production at all in M[A, a], then set M[A, a] to error (which we normally represent by an empty entry in the table).
1. 2.

47.)What do you mean by Handle?


Informally, a "handle" is a substring that matches the body of a production, and whose reduction represents one step along the reverse of a rightmost derivation. Formally, if S derives d w A i b t e h n u r i i c n v h a e t i o n , then production w s A -> in the w position following is a handle of w. Alternatively, a handle of a

right-sentential form is a production A-> and a position of where the string may be found, such that replacing at that position by A produces the previous right-sentential form in a rightmost derivation of .

52.)What do you mean by Shift-Reduce Parsing? Why this is called Shift-Reduce Parsing? Explain With Example.
A shift-reduce parser scans and parses the input text in one forward pass over the text, without backing up. The parser builds up the parse tree incrementally, bottom up, and left to right, without guessing or backtracking. At every point in this pass, the parser has accumulated a list of subtrees or phrases of the input text that have been already parsed. Those subtrees are not yet joined together because the parser has not yet reached the right end of the syntax pattern that will combine them.

A shift-reduce parser works by doing some combination of Shift steps and Reduce steps, hence the name.

A Shift step advances in the input stream by one symbol. That shifted symbol becomes a new single-node parse tree. A Reduce step applies a completed grammar rule to some of the recent parse trees, joining them together as one tree with a new root symbol.

The parser continues with these steps until all of the input has been consumed and all of the parse trees have been reduced to a single tree representing an entire legal input. In other words in Shift-reduce parsing a stack holds grammar symbols and an input buffer holds the
rest of the string to be parsed. During a left-to-right scan of the input string, the parser shifts zero or more input symbols onto the stack, until it is ready to reduce a string of grammar symbols on top of the stack. It then reduces to the head of the appropriate production. The parser repeats this cycle until it has detected an error or until the stack contains the start symbol and the input is empty. For eg :- Suppose we have the following grammar

Assign id = Sums Sums Sums + Products Sums Products Products Products * Value Products Value Value int Value id Then a shift reduce parser will generate the following parse:Step 0 1 2 3 4 5 Parse Ahead empty id id= id=id id=Value id=Products Lookahead id = id + + + Unscanned =B+C*2 B+C*2 +C*2 C*2 C*2 C*2 Parse Action Shift Shift Shift Reduce by Value<-id Reduce by Products<-Value Reduce by Sums<-Products

6 7 8 9 10 11 12 13 14 15 16

id=Sums id=Sums+ id=Sums+id id=Sums+Value id=Sums+Products id=Sums+Products* id=Sums+Products*int id=Sums+Products*Value id=Sums+Products id=Sums Assign

+ id * * * int eof eof eof eof eof

C*2 *2 2 2 2 eof

Shift Shift Reduce by Value<-id Reduce by Products<-Value Shift Shift Reduce by Value<-int Reduce by Products<Products*Value Reduce by Sums<-Sums+ Products Reduce by Assign<-id=Sums Done

57.)Explain Operator-Precedence Parser (algorithm with example)? Operator grammars have the property that no production on the right side is empty or has two adjacent non-terminals. This property enables the implementation of efficient operatorprecedence parsers. These parsers rely on the following three precedence relations: Relation a < b a = b a > b Meaning a yields precedence to b a has the same precedence as b a takes precedence over b

These operator precedence relations allow to delimit the handles in the right sentential forms: < marks the left end, = appears in the interior of the handle, and > marks the right end. Let assume that between the symbols ai and ai+1 there is exactly one precedence relation. Suppose that $ is the end of the string. Then for all terminals we can write: $ < b and b > $. If we remove all non-terminals and place the correct precedence relation: <, =, > between the remaining terminals, there remain strings that can be analysed by easily developed parser. Having precedence relations allows to identify handles as follows: - scan the string from left until seeing > - scan backwards the string from right to left until seeing < - everything between the two relations < and > forms the handle

Operator Precedence Parsing Algorithm

Initialize: Set ip to point to the first symbol of w$ Repeat: Let X be the top stack symbol, and a the symbol pointed to by ip if $ is on the top of the stack and ip points to $ then return else Let a be the top terminal on the stack, and b the symbol pointed to by ip if a < b or a = b then push b onto the stack advance ip to the next input symbol else if a > b then repeat pop the stack until the top stack terminal is related by < to the terminal most recently popped else error() end 62.) What is canonical LR(0) collection? An LR parser makes shift-reduce decisions by maintaining states to keep track of where we are in a parse. States represent sets of "items." An LR(0) item (item for short) of a grammar G is a production of G with a dot at some position of the body. Thus, production A -> XYZ yields the four items:A->.XYZ A->X.YZ A->XY.Z A->XYZ. Intuitively, an item indicates how much of a production we have seen at a given point in the parsing process. For example, the item A -> .XYZ indicates that we hope to see a string derivable from XYZ next on the input. One collection of sets of LR(0) items, called the canonical LR(0) collection, provides the basis for constructing a deterministic finite automaton that is used to make parsing decisions. Such an automaton is called an LR(0) automaton. In particular, each state of the LR(0) automaton represents a set of items in the canonical LR(0) collection.

You might also like