Syntax Analysis or Parsing
Syntax Analysis or Parsing
Syntax analyzer performs the process of parsing during the second phase of compilation process.
The parser uses the tokens produced by the lexical analyzer to create the tree like intermediate representation that depicts the grammatical structure of the token stream.
This tree is known as syntax tree or parse in which an interior node is represent an operation while the children of the node represent the argument of the operation.
A syntax analyzer or parser for a grammar G is a program, that takes as input a string w and produces as output either a parse tree for w if wL(G) or announces a syntax error if w L(G)
The parser is also called as recognizer who accept given grammar as source text for input and outputs success or failure as result .
Success corresponds to those cases where the input source text satisfies the grammar while failure is due to invalid inputs.
The useful of parser is recognized if it is capable of generating error messages explaining the negative outcomes.
The parser must performs the following tasks: Carry out context free syntax analysis and validate language constructs used for programming. Interact with scanner and ask for token when needed. Assist in translation by producing parse tree from which intermediate code can be generated that enables speedy target code generation. Flag meaningful error messages and attempt to correct them wherever possible.
In general, parsing a program generates the hierarchical correspondence between the program and the syntax rules. It is usually specified by tree like structure called derivation tree for the program and guide code generation.
Practically all programming languages are based on CFG. Parsing is required for either deriving a string from non terminal or reducing the string to non terminal. There are two fundamental strategies to parsing 1.Top-down parsing 2.Bottom -up Parsing A third approach called universal parsing also exists, but it is not practiced as it is not inefficient. Top down parsing is simple strategy while bottom parsing includes various advanced algorithm.
Top down parsing starts with distinguished start symbol of the grammar and expands on the remaining non terminals by replacing it with the Right-Hand Side(RHS) of one of the production until the desired string is obtained. For the valid source string , the top-down derivation sequence is as follows S=>=>.=>.=> Top down parsers are usually implemented as recursive descent parsers, or LL(K) predictive parsers.
LL(K) stands for left to right scan of the input, producing a leftmost derivation, with at most k symbols lookahead on the input. Example Consider a simple grammar that recognizes strings containing number of as followed by at least one b. SAC AaA| Cb|bC
using SAC using AaA using AaA using AaA using A using Cb
Bottlenecks in Top down parsing 1.Problem of backtracking 2.Problem of recognition 3.Problem of error detection 4.Problem of left recursion