cs160-lec4

cs160-lec4 - 1 CMPSC 160 Translation of Programming...

Info iconThis preview shows pages 1–5. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CMPSC 160 Translation of Programming Languages Lecture 4: Introduction to Parsing The Front End: Parser Parser Input: A sequence of tokens representing the source program Output: A parse tree (in practice, an abstract syntax tree) While generating the parse tree, parser checks the stream of tokens for grammatical correctness Checks the context-free syntax Parser builds an IR representation of the code Generates an abstract syntax tree Guides checking at deeper levels than syntax Source code Scanner IR Parser IR Type Checker Errors token get next token 2 Specifying Syntax with a Grammar Need a mathematical model of syntax a grammar G Context-free grammars Need an algorithm for testing membership in L(G) Parsing algorithms Parsing is the process of discovering a derivation for some sentence from the rules of the grammar Equivalently, it is the process of discovering a parse tree Natural language analogy Lexical rules correspond to rules that define the valid words Grammar rules correspond to rules that define valid sentences Specifying Syntax with a Grammar Context-free syntax is specified with a context-free grammar Formally, a grammar is a four-tuple, G = (S,N,T,P) T is a set of terminal symbols These correspond to tokens returned by the scanner For the parser, tokens are indivisible units of syntax N is a set of non-terminal symbols These are syntactic variables that can be substituted during a derivation Variables that denote sets of substrings occurring in the language S is the start symbol : S N All the strings in L(G) are derived from the start symbol P is a set of productions or rewrite rules : P : N (N T)* 3 An Example Grammar 1 Start Expr 2 Expr Expr Op Expr 3 | num 4 | id 5 Op + 6 |- 7 | * 8 | / Start symbol: S = Start Non-terminal symbols: N = { Start, Expr, Op } Terminal symbols: T = { num, id, +, -, *, / } Productions: P = { 1, 2, 3, 4, 5, 6, 7, 8 } (shown above) Context Free Grammar Programming languages have a set of rules that describe the syntactic structure of well formed programs A context free grammar is precise and understandable, yet powerful enough to express these rules It is so effective because it embraces the recursive nature of most programming languages Example sentence: if(x){ if(y){ if(z) { } } } Example grammar: B if ( id ) { B } This requires a variable number of states and is thus beyond the ability of regular expressions 4 Vocabulary Sentence of G: String of terminals in L(G) Sentential Form...
View Full Document

This note was uploaded on 11/23/2010 for the course MATH 104b taught by Professor Ceniceros,h during the Spring '08 term at UCSB.

Page1 / 12

cs160-lec4 - 1 CMPSC 160 Translation of Programming...

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

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