07-Grammars-Parsing - CS 421 Lecture 7: Grammars and...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CS 421 Lecture 7: Grammars and parsing s Announcements s MP2 review s Lecture outline s Context-free grammars s Top-down, a.k.a. recursive descent, parsing 6/14/2009 1 Announcements s TA office hours s I2CS: Tue, Thu 4-5pm CDT s On-campus: Wed 4-5pm CDT s MP2 solutions posted 6/14/2009 2 MP2 review s Problem 7 flatten : a list list -> a list flatten [[1;2;3]; [4;5]; [8;2;3;4]];; let rec flatten lst = match lst with 6/14/2009 3 MP2 review s Problem 7 flatten : a list list -> a list flatten [[1;2;3]; [4;5]; [8;2;3;4]];; let rec flatten lst = match lst with -> | ::xs -> flatten xs | (x::xs)::ys -> x::(flatten (xs::ys));; 6/14/2009 4 Review: compiler front-end 6/14/2009 5 Tokens Source Lexer AST Parser Intro to grammars and languages s Grammar s Finite set of t e r m i n a l s s Finite set of n o n - t e r m i n a l s s Finite set of p r o d u c t i o n r u l e s s Start symbol s Language s Set of strings recognized by a grammar 6/14/2009 6 Grammars: Chomsky hierarchy s Unrestricted s Recursively-enumerable languages s Recognized by a Turing machine s Context-sensitive s Context-sensitive languages s Recognized by a linear bounded automaton (LBA) s Context-free s Context-free languages s Recognized by a push-down automaton (PDA) s Regular s Regular languages s Recognized by a finite state automaton (FSA) 6/14/2009 7 Context-free grammar s Given: s Set of terminals (tokens) T s Set of non-terminals (variables) V s A cfg G is a set of p r o d u c t i o n s of the form s A X 1 X n ( n 0) where s A V , X 1 X n G = V T s One symbol designated as start symbol 6/14/2009 8 Notation s A X 1 X n s Also written A ::= X 1 X n s When n = 0, write A s Instead of A s When there is more than one production from A , say s A X 1 X n and A Y 1 Y n s Instead write: A X 1 X n | Y 1 Y n 6/14/2009 9 Example s Expressions s Exp intlit | variable | Exp + Exp | Exp * Exp s Sentences include s 3 s x s 3+x s 3+x*y s Tree representation 6/14/2009 10 Example s Method definition: MethodDef Type ident ( Args ) { Stmtlist } Args | NonEmptyArgs NonEmptyArgs Type ident | Type ident , NonEmptyArgs Stmtlist | Stmt Stmtlist Type ident | int | boolean s Sentence: int fun(boolean b) { } s Tree representation s ??...
View Full Document

Page1 / 33

07-Grammars-Parsing - CS 421 Lecture 7: Grammars and...

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

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