Parsing

Parsing - Syntax and Parsing Textbook, Sections 2.1,...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSC 4101: Programming Languages 1 1 Syntax and Parsing Textbook, Sections 2.1, 2.2.1-2.2.2, 2.3.1 Grammar vs. Parsing Code Grammar: X a Y b Z c Parser: Node* parseX() { ch = getNextToken(); if (ch!='a') error(". .."); y = parseY(); ch = getNextToken(); if (ch!='b') error(". .."); z = parseZ(); ch = getNextToken(); if (ch!='c') error(". .."); return new X(y, z); } 2 Context-Free Grammars Definition: Terminal symbols (tokens) id, num, -, (, ), +, *, / Non-terminal symbols exp, op Start symbol exp Rules N -> X . .. where N is a non-terminal and X ’s are (non-) terminals 3
Background image of page 1

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

View Full DocumentRight Arrow Icon
CSC 4101: Programming Languages 2 Context-Free Grammar Example exp -> id | num | - exp | ( exp ) | exp op exp op -> + | - | * | / 4 Derivations Input x * y + z Derivation exp => exp op exp => exp op id => exp + id => exp op exp + id => exp op id + id => exp * id + id => id * id + id 5 Derivations (cont’d) Left-most derivations = top-down parsing (recursive descent, LL(1)) Right-most derivations = bottom-up parsing (yacc, bison, etc., LR(1), LALR(1)) 6
Background image of page 2
CSC 4101: Programming Languages 3 Parse Trees x * y + z exp exp exp op exp exp op exp exp op exp + id id * exp op exp zx id * id id + id x y y z 7
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Parsing - Syntax and Parsing Textbook, Sections 2.1,...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online