Syntactic Analysis (Parsing) - Computer Science …rferrericancho/teaching/CL/parser1.pdf ·...

96
Syntactic Analysis (Parsing) Jos ´ e Miguel Rivero [email protected] Barcelona School of Informatics (FIB) Technical University of Catalonia (UPC) Jos ´ e Miguel Rivero. Syntax Analysis (Parsing) – p. 1/17

Transcript of Syntactic Analysis (Parsing) - Computer Science …rferrericancho/teaching/CL/parser1.pdf ·...

  • Syntactic Analysis (Parsing)Jose Miguel Rivero

    [email protected]

    Barcelona School of Informatics (FIB)

    Technical University of Catalonia (UPC)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 1/17

  • Summary

    Objectives of Syntax Analysis

    Context Free Grammars. Applications

    Parsing in Compilers / Interpreters

    Syntax vs. Semantics

    Derivation. Parse Tree

    Cocke-Younger-Kasami Parsing Algorithm

    Parse Tree and Abstract Syntax Tree (AST)

    Ambiguous Grammars

    Linear Parsing Algorithms

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 2/17

  • Summary

    Objectives of Syntax Analysis

    Context Free Grammars. Applications

    Parsing in Compilers / Interpreters

    Syntax vs. Semantics

    Derivation. Parse Tree

    Cocke-Younger-Kasami Parsing Algorithm

    Parse Tree and Abstract Syntax Tree (AST)

    Ambiguous GrammarsExpressionsDangling else

    Linear Parsing Algorithms

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 2/17

  • Summary

    Objectives of Syntax Analysis

    Context Free Grammars. Applications

    Parsing in Compilers / Interpreters

    Syntax vs. Semantics

    Derivation. Parse Tree

    Cocke-Younger-Kasami Parsing Algorithm

    Parse Tree and Abstract Syntax Tree (AST)

    Ambiguous Grammars

    Linear Parsing AlgorithmsTop-down LL(1) parsersBottom-up LR(1) parsers

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 2/17

  • Summary

    Objectives of Syntax Analysis

    Context Free Grammars. Applications

    Parsing in Compilers / Interpreters

    Syntax vs. Semantics

    Derivation. Parse Tree

    Cocke-Younger-Kasami Parsing Algorithm

    Parse Tree and Abstract Syntax Tree (AST)

    Ambiguous Grammars

    Linear Parsing AlgorithmsTop-down LL(1) parsersBottom-up LR(1) parsers

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 2/17

  • Objectives of Syntax Analysis

    Given a CFG (Context Free Grammar) G, with startsymbol S, and a word w (a sequence of tokens),can w be generated by G ?

    w L(G) ? S w ?

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 3/17

  • Objectives of Syntax Analysis

    Given a CFG (Context Free Grammar) G, with startsymbol S, and a word w (a sequence of tokens),can w be generated by G ?

    w L(G) ? S w ?

    Analyze the sequence of tokens to determine theirgrammatical structure with respect to G.Compute the parse tree corresponding to the input

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 3/17

  • Objectives of Syntax Analysis

    Given a CFG (Context Free Grammar) G, with startsymbol S, and a word w (a sequence of tokens),can w be generated by G ?

    w L(G) ? S w ?

    Analyze the sequence of tokens to determine theirgrammatical structure with respect to G.Compute the parse tree corresponding to the input

    Detect, diagnose, and recovery from syntax errors

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 3/17

  • Objectives of Syntax Analysis

    Given a CFG (Context Free Grammar) G, with startsymbol S, and a word w (a sequence of tokens),can w be generated by G ?

    w L(G) ? S w ?

    Analyze the sequence of tokens to determine theirgrammatical structure with respect to G.Compute the parse tree corresponding to the input

    Detect, diagnose, and recovery from syntax errors

    Accepts some invalid constructs, filtered out by thesemantic analysis

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 3/17

  • Context Free Grammars

    Expressivity

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ExpressivityL1 = { a

    n bn | n 0 }

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ExpressivityL1 = { a

    n bn | n 0 }

    L2 = { w c w1 | w (a|b) }

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ExpressivityL1 = { a

    n bn | n 0 }

    L2 = { w c w1 | w (a|b) }

    L3 = { w c w | w (a|b) }

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ExpressivityL1 = { a

    n bn | n 0 }

    L2 = { w c w1 | w (a|b) }

    L3 = { w c w | w (a|b) }

    L4 = { an bm cn dm | n,m 0 }

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ExpressivityL1 = { a

    n bn | n 0 }

    L2 = { w c w1 | w (a|b) }

    L3 = { w c w | w (a|b) }

    L4 = { an bm cn dm | n,m 0 }

    Algebraic expressions involving numbers,operations + and , and left and right parentheses

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ExpressivityL1 = { a

    n bn | n 0 }

    L2 = { w c w1 | w (a|b) }

    L3 = { w c w | w (a|b) }

    L4 = { an bm cn dm | n,m 0 }

    Algebraic expressions involving numbers,operations + and , and left and right parenthesesConstructions of programming languages such asdeclarations, statements (assignment, if , while ,. . . ), expressions, etc.

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    Applications

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ApplicationsNatural language processing (NLP)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ApplicationsNatural language processing (NLP)Type-setting languages (nroff, postscript), documentand extensible markup languages (LATEX, SGML,XML, . . . )

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ApplicationsNatural language processing (NLP)Type-setting languages (nroff, postscript), documentand extensible markup languages (LATEX, SGML,XML, . . . )Query for databases and information systems (SQL,DataLog, LDAP, . . . )

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ApplicationsNatural language processing (NLP)Type-setting languages (nroff, postscript), documentand extensible markup languages (LATEX, SGML,XML, . . . )Query for databases and information systems (SQL,DataLog, LDAP, . . . )Logical sinthesis and simulation of electronic circuits(VHDL)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ApplicationsNatural language processing (NLP)Type-setting languages (nroff, postscript), documentand extensible markup languages (LATEX, SGML,XML, . . . )Query for databases and information systems (SQL,DataLog, LDAP, . . . )Logical sinthesis and simulation of electronic circuits(VHDL)Graphical or modelling languages (UML, EnergySystems Language)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Context Free Grammars

    ApplicationsNatural language processing (NLP)Type-setting languages (nroff, postscript), documentand extensible markup languages (LATEX, SGML,XML, . . . )Query for databases and information systems (SQL,DataLog, LDAP, . . . )Logical sinthesis and simulation of electronic circuits(VHDL)Graphical or modelling languages (UML, EnergySystems Language)Many applications come with their built-in language.For example, the scripting language for Adobe Flash(ActionScript)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 4/17

  • Parsing in Compilers / Interpreters

    Conceptual structure:

    codesource

    ANALYSIS LEXICAL tokens

    list of

    ANALYSIS

    ANALYSIS

    syntactic

    SYNTACTIC

    annotatedsyntactic

    treetree SEMANTIC

    (AST) (AST)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 5/17

  • Parsing in Compilers / Interpreters

    Conceptual structure:

    codesource

    ANALYSIS LEXICAL tokens

    list of

    ANALYSIS

    ANALYSIS

    syntactic

    SYNTACTIC

    annotatedsyntactic

    treetree SEMANTIC

    (AST) (AST)

    Usual structure:

    codesource

    ANALYSISLEXICAL

    syntactic

    SEMANTICANALYSIS

    annotated

    treesyntactic

    SYNTACTIC

    token tree

    nextToken()

    ANALYSIS (AST) (AST)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 5/17

  • Parsing in Compilers / Interpreters

    Conceptual structure:

    codesource

    ANALYSIS LEXICAL tokens

    list of

    ANALYSIS

    ANALYSIS

    syntactic

    SYNTACTIC

    annotatedsyntactic

    treetree SEMANTIC

    (AST) (AST)

    Usual structure:

    codesource

    ANALYSISLEXICAL

    syntactic

    SEMANTICANALYSIS

    annotated

    treesyntactic

    SYNTACTIC

    token tree

    nextToken()

    ANALYSIS

    First pass Second pass

    (AST) (AST)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 5/17

  • Syntax vs. Semantics

    My uncles John dawn her jeans

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Syntax vs. Semantics

    My uncles John dawn her jeansS

    NP VP

    Adj Attr N V NP

    N Adj N

    My uncles John dawn her jeans

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Syntax vs. Semantics

    My uncles John dawn her jeansS

    NP VP

    Adj Attr N V NP

    N Adj N

    My uncles John dawn her jeans

    Marketplace for : input the a .

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Syntax vs. Semantics

    My uncles John dawn her jeansS

    NP VP

    Adj Attr N V NP

    N Adj N

    My uncles John dawn her jeans

    Marketplace for : input the a .

    They are hunting dogs

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Syntax vs. Semantics

    Programming languages.Syntax errors:

    public class {int static i;boolean j;public double i(UnknownClass k {

    i..x = "hello";if (i / 2)

    i + 1 = j;return i > ;

    }}

    }

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Syntax vs. Semantics

    Programming languages.Syntax errors:

    public class {int static i;boolean j;public double i(UnknownClass k {

    i. . x = "hello";if (i / 2)

    i + 1 = j;return i > ;

    }}

    }

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Syntax vs. Semantics

    Programming languages.Semantic errors:

    public class MyFirstClass {static int i;boolean j;public double i(UnknownClass k) {

    i.x[10] = "hello";if (i / 2)

    i + 1 = j;return i > 3.14;

    }}

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Syntax vs. Semantics

    Programming languages.Semantic errors:

    public class MyFirstClass {static int i ;boolean j;public double i ( UnknownClass k) {

    i.x[10] = "hello";if ( i / 2 )

    i + 1 = j;return i > 3.14;

    }}

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Syntax vs. Semantics

    Programming languages.Ambiguity:

    class Bar {public float foo( ) {

    return 1;}

    public int foo( ) {return 2;

    }};

    float x = Bar::foo ( );

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 6/17

  • Derivation. Parse Tree

    Grammar G :

    S aABe

    A Abc | b

    B d

    w = abbcde

    w L(G) ? S abbcde ?

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 7/17

  • Derivation. Parse Tree

    Grammar G :

    S aABe

    A Abc | b

    B d

    w = abbcde

    S

    Parse tree: S

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 7/17

  • Derivation. Parse Tree

    Grammar G :

    S aABe

    A Abc | b

    B d

    w = abbcde

    S aABe

    Parse tree: S

    a A B e

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 7/17

  • Derivation. Parse Tree

    Grammar G :

    S aABe

    A Abc | b

    B d

    w = abbcde

    S aABe aAbcBe

    Parse tree: S

    a A B e

    A b c

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 7/17

  • Derivation. Parse Tree

    Grammar G :

    S aABe

    A Abc | b

    B d

    w = abbcde

    S aABe aAbcBe abbcBe

    Parse tree: S

    a A B e

    A b c

    b

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 7/17

  • Derivation. Parse Tree

    Grammar G :

    S aABe

    A Abc | b

    B d

    w = abbcde

    S aABe aAbcBe abbcBe abbcde = w

    Parse tree: S

    a A B e

    A b c d

    b

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 7/17

  • Derivation. Parse Tree

    Grammar G :

    S aABe

    A Abc | b

    B d

    w = abbcde

    S aABe aAbcBe abbcBe abbcde = w

    Parse tree: S

    a A B e

    A b c d

    b

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 7/17

  • Cocke-Younger-Kasami Algorithm

    Input : A CFG in Chomsky Normal Form G, and a word w = a1 . . . an

    Only rules of the form A a or A B C

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 8/17

  • Cocke-Younger-Kasami Algorithm

    Input : A CFG in Chomsky Normal Form G, and a word w = a1 . . . an

    Only rules of the form A a or A B C

    Output : An n n table T, where T [i, l] is the set of nonterminals A

    that generate the substring ai . . . ai+l1

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 8/17

  • Cocke-Younger-Kasami Algorithm

    Input : A CFG in Chomsky Normal Form G, and a word w = a1 . . . an

    Only rules of the form A a or A B C

    Output : An n n table T, where T [i, l] is the set of nonterminals A

    that generate the substring ai . . . ai+l1

    Example : w = a1a2 . . . a8

    1 2 3 4 l = 5 6 7 8

    1

    2

    i = 3 B A

    4

    5 C

    6

    7

    8

    A B C G

    B a3a4 C a5a6a7

    A B C a3a4a5a6a7

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 8/17

  • Cocke-Younger-Kasami Algorithm

    Input : A CFG in Chomsky Normal Form G, and a word w = a1 . . . an

    Only rules of the form A a or A B C

    Output : An n n table T, where T [i, l] is the set of nonterminals A

    that generate the substring ai . . . ai+l1

    Algorithm :

    for all i [1 .. n] : T [i, 1] := {A | A ai G}

    for all l [2 .. n] :

    for all i [1 .. n l] :

    for every A B C G :

    if there is a l1 [1 .. l 1] such that

    B T [i, l1] and C T [i + l1, l l1]

    then

    T [i, l] := T [i, l] {A}

    Using dynamic program-ming, each table entrycan be filled in O(n)time.The algorithm runs inO(n3) time.

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 8/17

  • Question

    Using the Cocke-Younger-Kasami (CYK) algorithm,how to determine that w = a1 . . . an is in the language?

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 9/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #1 :

    E

    Parse Tree:E

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #1 :

    E ld

    E + E

    Parse Tree:E

    E + E

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #1 :

    E ld

    E + E ldid + E

    Parse Tree:E

    E + E

    id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #1 :

    E ld

    E + E ldid + E

    ldid + E E

    Parse Tree:E

    E + E

    id E E

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #1 :

    E ld

    E + E ldid + E

    ldid + E E

    ldid + id E

    Parse Tree:E

    E + E

    id E E

    id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #1 :

    E ld

    E + E ldid+ E

    ldid+ E E

    ldid+ id E

    ldid+ id id

    Parse Tree:E

    E + E

    id E E

    id id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #1 :

    E ld

    E + E ldid+ E

    ldid+ E E

    ldid+ id E

    ldid+ id id

    Parse Tree:E

    E + E

    id E E

    id id

    id1 + (id2 id3)

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #1 :

    E ld

    E + E ldid+ E

    ldid+ E E

    ldid+ id E

    ldid+ id id

    Parse Tree:

    E

    E + E

    id E E

    id id

    id1 + (id2 id3)

    Abstract Syntax Tree (AST):

    +

    id1

    id2 id3

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #2 :

    E

    Parse Tree:E

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #2 :

    E ld

    E E

    Parse Tree:E

    E E

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #2 :

    E ld

    E E ld

    E + E E

    Parse Tree:E

    E E

    E + E

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #2 :

    E ld

    E E ld

    E + E E ldid + E E

    Parse Tree:E

    E E

    E + E

    id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #2 :

    E ld

    E E ld

    E + E E ldid + E E

    ldid + id E

    Parse Tree:E

    E E

    E + E

    id id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #2 :

    E ld

    E E ld

    E +E E ldid+E E

    ldid+idE

    ldid+idid

    Parse Tree:E

    E E

    E + E id

    id id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #2 :

    E ld

    E E ld

    E +E E ldid+E E

    ldid+idE

    ldid+idid

    Parse Tree:E

    E E

    E + E id

    id id

    (id1 + id2) id3

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Parsing expressions

    G : E E + E | E E | (E) | id w = id1 + id2 id3

    (Leftmost) Derivation #2 :

    E ld

    E E ld

    E +E E ldid+E E

    ldid+idE

    ldid+idid

    Parse Tree:

    E

    E E

    E + E id

    id id

    (id1 + id2) id3

    Abstract Syntax Tree (AST):

    + id3

    id1 id2

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 10/17

  • Ambiguous grammar

    A grammar G is said to be ambiguous if thereis some string w that has more than one parsetree or more than one leftmost derivation.

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 11/17

  • Ambiguous grammar

    G : E E + E | E E | ( E ) | idw = id1 + id2 id3

    Parse Tree A:

    E

    E + E

    id E E

    id id

    id1 + (id2 id3)

    Parse Tree B:

    E

    E E

    E + E id

    id id

    (id1 + id2) id3

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 11/17

  • Operators precedence

    id1 + id2 id3 id4 + id5

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 12/17

  • Operators precedence

    id1 + (id2 id3 id4) + id5

    Precedence: + p

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 12/17

  • Operators precedence

    id1 + (id2 id3 id4) + id5

    Precedence: + p

    This other grammar G

    reflects the operatorsprecedence:

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 12/17

  • Operators precedence

    id1 + (id2 id3 id4) + id5

    Precedence: + p

    This other grammar G

    reflects the operatorsprecedence:

    E E + E | TT T T | FF ( E ) | id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 12/17

  • Operators precedence

    id1 + (id2 id3 id4) + id5

    Precedence: + p

    This other grammar G

    reflects the operatorsprecedence:

    E E + E | TT T T | FF ( E ) | id

    Now w = id1 + id2 id3only has this parse tree:

    id1 + (id2 id3)

    E

    E + E

    T T

    F T T

    id F F

    id id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 12/17

  • Operators precedence

    id1 + (id2 id3 id4) + id5

    Precedence: + p

    This other grammar G

    reflects the operatorsprecedence:

    E E + E | TT T T | FF ( E ) | id

    Now w = id1 id2 + id3only has this parse tree:

    (id1 id2) + id3

    E

    E + E

    T T

    T T F

    F F id

    id id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 12/17

  • Operators precedence

    id1 + (id2 id3 id4) + id5

    Precedence: + p

    This other grammar G

    reflects the operatorsprecedence:

    E E + E | TT T T | FF ( E ) | id

    But w = id1 + id2 + id3still has two parse trees:

    (id1 + id2) + id3

    E

    E + E

    E + E T

    T T F

    F F id

    id id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 12/17

  • Operators precedence

    id1 + (id2 id3 id4) + id5

    Precedence: + p

    This other grammar G

    reflects the operatorsprecedence:

    E E + E | TT T T | FF ( E ) | id

    But w = id1 + id2 + id3still has two parse trees:

    id1 + (id2 + id3)

    E

    E + E

    T E + E

    F T T

    id F F

    id id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 12/17

  • Operators associativity

    Left-associative:e1 e2 e3 means (e1 e2) e3

    Right-associative:e1 e2 e3 means e1 (e2 e3)

    Non-associative:e1 e2 e3 is not correct

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 13/17

  • Operators associativity

    Left-associative:e1 e2 e3 means (e1 e2) e3

    Right-associative:e1 e2 e3 means e1 (e2 e3)

    Non-associative:e1 e2 e3 is not correct

    Usually arithmetic (+, - , * , / ) and logical operators (&&, || )are left-associative, comparison operators (>, = ,

  • Operators associativity

    id1 id2 + id3 id4 + id5

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 13/17

  • Operators associativity

    (((id1 id2) + id3) id4) + id5

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 13/17

  • Operators associativity

    (((id1

    E

    id2

    E

    ) + id3

    E

    ) id4

    E

    ) + id5 E

    E + id5

    E id4

    E + id3

    E id2

    id1

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 13/17

  • Operators associativity

    Left-associativity (+, ):E E + id

    | E id

    | id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 13/17

  • Operators associativity

    Left-associativity (+, ):E E + id

    | E id

    | id

    Right-associativity (+, ):E id + E

    | id E

    | id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 13/17

  • Operators associativity

    Left-associativity (+, ):E E + id

    | E id

    | id

    Right-associativity (+, ):E id + E

    | id E

    | id

    Left-associativity (+, ):E E + T

    | T

    T T F

    | F

    F ( E )

    | id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 13/17

  • Grammar for expressions

    Grammar G :

    E E + T

    | T

    T T F

    | F

    F ( E )

    | id

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 14/17

  • Grammar for expressions

    Grammar G :

    E E + T

    | T

    T T F

    | F

    F ( E )

    | id

    w = id1 id2 ( id3 + id4 )

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 14/17

  • Grammar for expressions

    Grammar G :

    E E + T

    | T

    T T F

    | F

    F ( E )

    | id

    w = id1 id2

    ( id3 + id4

    )

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 14/17

  • Grammar for expressions

    Grammar G :

    E E + T

    | T

    T T F

    | F

    F ( E )

    | id

    w = id1 id2

    ( id3 + id4

    )

    E

    T

    T F

    T F ( E )

    F id2 E + T

    id1 T F

    F id4

    id3

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 14/17

  • Grammar for expressions

    Grammar G :

    E E + T

    | T

    T T F

    | F

    F ( E )

    | id

    w = id1 id2

    ( id3 + id4

    )

    Abstract Syntax Tree(AST) :

    +

    id1 id2 id3 id4

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 14/17

  • Exercises

    Extend the grammar for expressions with the followingoperators and precedence:

    {>,

  • Exercises

    Extend the grammar for expressions with the followingoperators and precedence:

    {>,

  • Ambiguous If-then-else (dangling else)

    Grammar:

    P if C then P

    | if C then P else P

    | p

    C c

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 16/17

  • Ambiguous If-then-else (dangling else)

    Grammar: w = if c1

    then if c2

    then p1

    else p2

    P if C then P

    | if C then P else P

    | p

    C c

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 16/17

  • Ambiguous If-then-else (dangling else)

    Grammar: w = if c1

    then if c2

    then p1

    else p2

    P if C then P

    | if C then P else P

    | p

    C c

    Parse Tree A: P

    if C then P

    c1

    if C then P else P

    c2

    p1

    p2

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 16/17

  • Ambiguous If-then-else (dangling else)

    Grammar: w = if c1

    then if c2

    then p1

    else p2

    P if C then P

    | if C then P else P

    | p

    C c

    Abstract Syntax Tree (AST) A:

    if -then

    c1

    if -then-else

    c2

    p1

    p2

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 16/17

  • Ambiguous If-then-else (dangling else)

    Grammar: w = if c1

    then if c2

    then p1

    else p2

    P if C then P

    | if C then P else P

    | p

    C c

    Parse Tree B: P

    if C then P else P

    c1

    if C then P p2

    c2

    p1

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 16/17

  • Ambiguous If-then-else (dangling else)

    Grammar: w = if c1

    then if c2

    then p1

    else p2

    P if C then P

    | if C then P else P

    | p

    C c

    Abstract Syntax Tree (AST) B:

    if -then-else

    c1

    if -then p2

    c2

    p1

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 16/17

  • Exercise

    Find an alternative non-ambiguous grammarfor this language.

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 17/17

  • Exercise

    Find an alternative non-ambiguous grammarfor this language.

    Give a clue?There are two kind of propositions:

    closed, where a following else cannot correspondto them.open, where a following else will correspond tothem (to their if ).

    Jose Miguel Rivero. Syntax Analysis (Parsing) p. 17/17

    SummarySummarySummarySummary

    Objectives of Syntax AnalysisObjectives of Syntax AnalysisObjectives of Syntax AnalysisObjectives of Syntax Analysis

    Context Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free GrammarsContext Free Grammars

    Parsing in Compilers / InterpretersParsing in Compilers / InterpretersParsing in Compilers / Interpreters

    Syntax vs.@ SemanticsSyntax vs.@ SemanticsSyntax vs.@ SemanticsSyntax vs.@ SemanticsSyntax vs.@ SemanticsSyntax vs.@ SemanticsSyntax vs.@ SemanticsSyntax vs.@ SemanticsSyntax vs.@ Semantics

    Derivation. Parse TreeDerivation. Parse TreeDerivation. Parse TreeDerivation. Parse TreeDerivation. Parse TreeDerivation. Parse TreeDerivation. Parse Tree

    Cocke-Younger-Kasami AlgorithmCocke-Younger-Kasami AlgorithmCocke-Younger-Kasami AlgorithmCocke-Younger-Kasami Algorithm

    QuestionParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressionsParsing expressions

    Ambiguous grammarAmbiguous grammar

    Operator's precedenceOperator's precedenceOperator's precedenceOperator's precedenceOperator's precedenceOperator's precedenceOperator's precedenceOperator's precedence

    Operator's associativityOperator's associativityOperator's associativityOperator's associativityOperator's associativityOperator's associativityOperator's associativityOperator's associativity

    Grammar for expressionsGrammar for expressionsGrammar for expressionsGrammar for expressionsGrammar for expressions

    ExercisesExercises

    Ambiguous $;${t f If-then-else} $;$(dangling {t f else}) Ambiguous $;${t f If-then-else} $;$(dangling {t f else}) Ambiguous $;${t f If-then-else} $;$(dangling {t f else}) Ambiguous $;${t f If-then-else} $;$(dangling {t f else}) Ambiguous $;${t f If-then-else} $;$(dangling {t f else}) Ambiguous $;${t f If-then-else} $;$(dangling {t f else})

    ExerciseExercise