03._Parsing

# 03._Parsing - Parsing Teodor Przymusinski UCR Compiler...

This preview shows pages 1–7. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Parsing Teodor Przymusinski, UCR Compiler Construction CS152 - Based on the slides by Leonidas Fegaras, UTA Parser A parser recognizes sequences of tokens according to some grammar and generates Abstract Syntax Trees (ASTs) A context-free grammar (CFG) has a finite set of terminals (tokens) a finite set of nonterminals from which one is the start symbol and a finite set of productions of the form: A ::= X1 X2 ... Xn where A is a nonterminal and each Xi is either a terminal or nonterminal symbol scanner parser get token token source file get next character AST 2 Compiler Construction CS152 - Based on the slides by Leonidas Fegaras, UTA Example Expressions: E ::= E + T | E - T | T T ::= T * F | T / F | F F ::= num | id Nonterminals: E T F Start symbol: E Terminals: + - * / id num – ... or equivalently: E ::= E + T E ::= E - T E ::= T T ::= T * F T ::= T / F T ::= F F ::= num F ::= id 3 Compiler Construction CS152 - Based on the slides by Leonidas Fegaras, UTA Derivations Notation: terminals: t, s, ... nonterminals: A, B, ... symbol (terminal or nonterminal): X, Y, ... sequence of symbols: a, b, ... Given a production: A ::= X 1 X 2 ... X n the form aAb => aX 1 X 2 ... X n b is called a derivation eg, using the production T ::= T * F we get T / F + 1 - x => T * F / F + 1 - x Leftmost derivation: when you always expand the leftmost nonterminal in the sequence Rightmost derivation: ... rightmost nonterminal 4 Compiler Construction CS152 - Based on the slides by Leonidas Fegaras, UTA Top-down Parsing It starts from the start symbol of the grammar and applies derivations until the entire input string is derived Example that matches the input sequence: id(x) + num(2) * id(y) or id + num * id : E => E + T use E ::= E + T => E + T * F use T ::= T * F => T + T * F use E ::= T => T + F * F use T ::= F => T + num * F use F ::= num => F + num * F use T ::= F => id + num * F use F ::= id => id + num * id use F ::= id You may have more than one choice at each derivation step: may have multiple nonterminals in each sequence for each nonterminal in the sequence, may have many rules to choose from Wrong predictions will cause backtracking need predictive parsing that never backtracks 5 Compiler Construction CS152 - Based on the slides by Leonidas Fegaras, UTA Bottom-up Parsing It starts from the input string and uses derivations in the opposite directions (from right to left) until you derive the start symbol Previous example: id(x) + num(2) * id(y) <= id(x) + num(2) * F use F ::= id <= id(x) + F * F use F ::= num <= id(x) + T * F use T ::= F <= id(x) + T use T ::= T * F <= F + T...
View Full Document

{[ snackBarMessage ]}

### Page1 / 33

03._Parsing - Parsing Teodor Przymusinski UCR Compiler...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online