csc402-ln002

csc402-ln002 - Grammars The most convenient way to describe...

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

View Full Document Right Arrow Icon
Grammars The most convenient way to describe the structure of programming languages is using a context-free grammar (often called CFG or BNF for Backus- Nauer Form ). Here we will simply refer to grammars with the understanding that we are referring to CFGs. Grammars can readily express the structure of phrases in programming languages return-stmt : ‘return’ expr ‘;’ if-stmt : ‘if’ expr ‘then’ stmt ‘else’ stmt ‘endif’
Background image of page 1

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

View Full DocumentRight Arrow Icon
Grammars Grammars have 4 parts to them 1. Non-terminal Symbols - these give names to phrase structures 2. Terminal Symbols - these give names to the tokens in a language (sometimes we don’t use explicit tokens but put the words that make up the tokens of a language in quotes) 3. Rules - these describe that actual structure of phrases in a language 4. Start Symbol - a special non-terminal that gives a name to the largest possible phrase(s) in the language (often denoted by an asterisk).
Background image of page 2
Example: The Exp0 Language grammar exp0; prog : stmt+ ; stmt : 'p' exp ';' | 's' lhsvar exp ';' ; exp : '+' exp exp | '-' exp exp | '(' exp ')' | rhs ! var | num ; lhsvar : 'x' | 'y' | 'z' ; rhs ! var : 'x' | 'y' | 'z' ; num : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' |'9' ; Start Symbol: prog
Background image of page 3

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

View Full DocumentRight Arrow Icon
A grammar tells us if a sentence belongs to the language, e.g. Does ‘s x 3 ;’ belong to the language? s
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/03/2011 for the course CSC 402 taught by Professor Staff during the Fall '09 term at Rhode Island.

Page1 / 19

csc402-ln002 - Grammars The most convenient way to describe...

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