1 / 6

Mapping Priorities to Trees in Recursive Descent Parsers - summary and exercise -

Mapping Priorities to Trees in Recursive Descent Parsers - summary and exercise -. Expressions with Two Operators. expr ::= ident | expr - expr | expr ^ expr | ( expr ). where: “-” is left-associative “^” is right-associative “^” has higher priority than “-”

ivan
Download Presentation

Mapping Priorities to Trees in Recursive Descent Parsers - summary and exercise -

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mapping Priorities to Trees in Recursive Descent Parsers- summary and exercise -

  2. Expressions with Two Operators expr ::= ident | expr - expr| expr^ expr | (expr) where: • “-” is left-associative • “^” is right-associative • “^” has higher priority than “-” Draw parentheses and a tree for token sequence: a – b – c ^ d ^ e – f ((a – b) – (c ^ (d ^ e)) ) – f

  3. Goal: Build Expressions • abstract class Expr • case class Variable(id : Identifier) extends Expr • case class Minus(e1 : Expr, e2 : Expr) extendsExpr • case class Exp(e1 : Expr, e2 : Expr) extends Expr

  4. 1) Layer the grammar by priorities expr ::= ident | expr - expr| expr^ expr | (expr) expr ::= term (- term)* term ::= factor (^ factor)* factor ::= id | (expr) lower priority binds weaker, so it goes outside

  5. 2) Build trees in the right way LEFT-associative operator x – y – z  (x – y) – z Minus(Minus(Var(“x”),Var(“y”)), Var(“z”)) defexpr : Expr = { var e = while (lexer.token == MinusToken) { lexer.next } e } term e = Minus(e, term)

  6. 2) Build trees in the right way RIGHT-associative operator – using a loop x ^ y ^ z  x ^ (y ^ z) Exp(Var(“x”), Exp(Var(“y”), Var(“z”)) ) defexpr : Expr = { val e = factor if (lexer.token == ExpToken) { lexer.next Exp(e, expr) } else e }

More Related