Syntax Analysis or Parsing

download Syntax Analysis or Parsing

of 11

Transcript of Syntax Analysis or Parsing

  • 7/29/2019 Syntax Analysis or Parsing

    1/11

    Also called as syntax analyzer or parser. Syntax analyzerperforms the process of parsing during the second phase ocompilation process.

    The parser uses the tokens produced by the lexical analyzer tocreate the tree like intermediate representation that depictsthe grammatical structure of the token stream.

    This tree is known as syntax tree or parse in which an interiornode is represent an operation while the children of the noderepresent the argument of the operation.

  • 7/29/2019 Syntax Analysis or Parsing

    2/11

    A syntax analyzer or parser for a grammar G is a program, thattakes as input a string w and produces as output either a parsetree for w if wL(G) or announces a syntax error if w L(G)

  • 7/29/2019 Syntax Analysis or Parsing

    3/11

    The parser is also called as recognizer who accept givengrammar as source text for input and outputs success or failureas result .

    Success corresponds to those cases where the input source textsatisfies the grammar while failure is due to invalid inputs.

    The useful of parser is recognized if it is capable of generatingerror messages explaining the negative outcomes.

  • 7/29/2019 Syntax Analysis or Parsing

    4/11

    The parser must performs the following tasks:

    Carry out context free syntax analysis and validate languageconstructs used for programming.

    Interact with scanner and ask for token when needed.Assist in translation by producing parse tree from whichintermediate code can be generated that enables speedy targetcode generation.

    Flag meaningful error messages and attempt to correct themwherever possible.

  • 7/29/2019 Syntax Analysis or Parsing

    5/11

  • 7/29/2019 Syntax Analysis or Parsing

    6/11

    In general, parsing a program generates the hierarchicalcorrespondence 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.

  • 7/29/2019 Syntax Analysis or Parsing

    7/11

    Practically all programming languages are based on CFG.

    Parsing is required for either deriving a string from nonterminal 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 isnot practiced as it is not inefficient.

    Top down parsing is simple strategy while bottom parsingincludes various advanced algorithm.

  • 7/29/2019 Syntax Analysis or Parsing

    8/11

    Top down parsing starts with distinguished start symbol of thegrammar and expands on the remaining non terminals byreplacing 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 derivationsequence is as follows

    S=>=>.=>.=>

    Top down parsers are usually implemented as recursivedescent parsers, or LL(K) predictive parsers.

  • 7/29/2019 Syntax Analysis or Parsing

    9/11

    LL(K) stands for left to right scan of the input, producing aleftmost derivation, with at most k symbols lookahead on theinput.

    ExampleConsider a simple grammar that recognizes strings containingnumber ofas followed by at least one b.

    SAC

    AaA|

    Cb|bC

  • 7/29/2019 Syntax Analysis or Parsing

    10/11

    SAC using SAC

    =>aAC using AaA

    =>aaAC using AaA

    =>aaaAC using AaA

    =>aaa C using A

    =>aaab using Cb

  • 7/29/2019 Syntax Analysis or Parsing

    11/11

    Bottlenecks in Top down parsing

    1.Problem of backtracking

    2.Problem of recognition

    3.Problem of error detection

    4.Problem of left recursion