Aug 29, 2003 ------------- - Syntax and Semantics - syntax defines structure of language - semantics defines meaning of language - Previously, both - used to be described intuitively - now, well-defined formal systems available - How an interpreter works - lexical analysis - syntax analysis - semantic analysis - What is a language? - set of strings over some alphabet - legal strings = sentences - Examples Lex | Syn | Sem - Tommy is a bod. No - Tommy boy is a. Yes No - Boy is a Tommy. Yes Yes No - Tommy is a boy. Yes Yes Yes - What does lexical analysis do? - parcel characters into lexemes - lexemes fall into "token" categories - Example: analyzing "int i = 20;" lexemes | tokens ------------------------- - int | reserved word - i | identfier - = | special symbol = - 20 | numeric constant - ; | special symbol ; - What property did we use? - delimiter = white space? - a.k.a. free-format language - Early versions of FORTRAN - were fixed format - DO 99 I = 1 . 10 is different from - DO 99 I = 1 , 10 ! - Grammars define how sentences are constructed - e.g., simple grammar for English - Tommy is a boy sentences. - parse tree and productions - Lets parse "Time flies like an arrow" - 4 different meanings! - we say that the grammar is ambiguous! - how can we determine intended meaning? - need context - Chomsky hierarchy of languages - regular languages - context-free languages (CFL) - context-sensitive languages (CSL) - phase structured languages - In this course - we will study CFLs - productions have only one "thing" on the left - called "non-terminals" - An example of a property that is not context-free - first letter must be capitalized - an example CSL for this! - exercise: try redoing it with a CFL! (hint: its possible) - A Grammar for a Calculator - for evaluating expressions