A compiler is a computer program (or set ofprograms) that transforms source codewritten in a programminglanguage(the source language) into anothercomputer language. The most commonreason for wanting to transform source codeis to create an executable program.
Source Program Lexical Analysis Syntax Analysis Intermediate codeTrade Management Error Handing generation Code Optimization Code generation Target Program
Lexical Analysis Phase:– The Lexical Analyzer reads the characters in the source program andgroups them into a stream of tokens in which each token represents a logicallycohesive sequence of characters, such as, An identifier, A keyword, Apunctuation character.Syntax Analysis Phase:– The Syntax Analyzer groups tokens together into syntactic structure.Consider, for Ex: the three tokens representing A+B might be grouped into asyntactic called an expression.Intermediate Code Generator:– The Intermediate Code Generator uses the structure produce by the syntaxanalyzer to create a stream of simple instructions. The primary differencebetween the intermediate code and assembly code is that the intermediatecode need not specify the register to be used for each operation.
Code Optimization:– Code Optimization in an optional phase designed to improve theintermediate code so that the ultimate program runs faster. Its output isanother intermediate code program that does the same job as the originalintermediate code.Code Generation:– The final phase of the compiler is the generation of target code, consistingnormally of relocatable machine code or assembly code. Memory locations areselected for each of the variables used by the program. Then, the eachintermediate instruction is translated into a sequence of machine instructionsthat perform the same task.
– A Parser for grammar G is a program that takes as input a string w andproduce as output either a parse tree for w, if w is a sentence of G, or an errormessage indicating that w is not a sentence of G.
There are Two basic types of Parsers:a) Bottom-upb) Top-down– As name indicates Bottom-Up Parsers build parse trees from the Bottom(leaves) to the Top (root), while top-down parsers starts with the root and workdown to the leaves. In both the cases the input to the parser is being scannedfrom left to right, considering ONE symbol at a time. A Bottom-Up Parsers try to find the right-most derivation of the given inputin Reverse order. S ... (the right-most derivation of ) (the bottom-up parser finds the right-most derivation in the reverse order)
Also Know as “Shift-reduce” parsingBottom-Up Parsing The bottom-up parsing method is also know as Shift-reduce parsing because its consist of two main actions. i.e shift and reduce.At each shift action, the current symbol in the input string is pushed to a stack.At each reduction step, the symbols at the top of the stack will replaced by thenon-terminal at the left side of that production.There are also two more actions: accept and error.
Shift-Reduce ParsingThere are four possible action of a shift-parse action are as follows:- 1. Shift : The next input symbol is shifted onto the top of the stack. 2. Reduce: Replace the handle on the top of the stack by the non-terminal. 3. Accept: Successful completion of parsing. 4. Error: Parser discovers a syntax error, and calls an error recovery routine.
A shift-reduce parser tries to reduce the given input string into the starting symbol. A string reduced to () the starting symbol At each reduction step, a substring of the input matching tothe right side of a production rule is replaced by the non-terminal atthe left side of that production rule. If the substring is chosen correctly, the right mostderivation of that string is created in the reverse order. Rightmost Derivation: S Shift-Reduce Parser finds: ... S
HANDLE• Informally, a handle of a string is a substring that matches the right side of a production rule. – But not every substring matches the right side of a production rule is handle• A handle of a right sentential form ( ) is a production rule A and a position of where the string may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of . S A • If the grammar is unambiguous, then every right-sentential form of the grammar has exactly one handle.• We will see that is a string of terminals.
HANDLE PRUNING:A right-most derivation in reverse can be obtained by handle-pruning. S=0 1 2 ... n-1 n= input stringStart from n, find a handle Ann in n,and replace n in by An to get n-1.Then find a handle An-1n-1 in n-1,and replace n-1 in by An-1 to get n-2.Repeat this, until we reach S.
Bottom –Up Parser: Consider for Example :- Grammar S aABe A Abc | b B d parse The input string : abbcde.
Bottom-Up Parser Example Shift a INPUT: a b b c d e $ OUTPUT: Production S aABe Bottom-Up Parsing A Abc Program A b Bd
Bottom-Up Parser Example Shift b Reduce from b to A INPUT: a b b c d e $ OUTPUT: Production S aABe Bottom-Up Parsing A Abc A Program A b Bd b
Bottom-Up Parser Example Shift A INPUT: a A b c d e $ OUTPUT: Production S aABe Bottom-Up Parsing A Abc A Program A b Bd b
Bottom-Up Parser Example Shift b INPUT: a A b c d e $ OUTPUT: Production S aABe Bottom-Up Parsing A Abc A Program A b Bd b
Shift c Reduce from Abc to A INPUT: a A b c d e $ OUTPUT:Production AS aABe Bottom-Up Parsing A Abc A b c Program A b Bd b
Bottom-Up Parser Example Shift A INPUT: a A d e $ OUTPUT: Production A S aABe Bottom-Up Parsing A Abc A b c Program A b Bd b
Bottom-Up Parser Example Shift d Reduce from d to B INPUT: a A d e $ OUTPUT: Production A B S aABe Bottom-Up Parsing A Abc A b c d Program A b Bd b
Bottom-Up Parser Example Shift B INPUT: a A B e $ OUTPUT: Production A B S aABe Bottom-Up Parsing A Abc A b c d Program A b Bd b
Bottom-Up Parser Example Shift e Reduce from aABe to S INPUT: a A B e $ OUTPUT: S Production e a A B S aABe Bottom-Up Parsing A Abc A b c d Program A b Bd b
Bottom-Up Parser Example Shift S Hit the target $ INPUT: S $ OUTPUT: S Production e a A B S aABe Bottom-Up Parsing A Abc A b c d Program A b Bd b