Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 3302 Programming Languages

Similar presentations


Presentation on theme: "CSE 3302 Programming Languages"— Presentation transcript:

1 CSE 3302 Programming Languages
Syntax Chengkai Li, Weimin He Spring 2008 Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

2 Phases of Compilation [Programming Language Pragmatics, by Michael Scott] Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

3 Syntax and Semantics Defining a programming language:
Specifications of syntax Syntax – structure (form) of programs (the form a program in the language must take). Specifications of semantics Semantics - the meaning of programs Precise definition, without ambiguity Given a program, there is only one unique interpretation. Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

4 Purpose of Describing Syntax and Semantics
For language designers: Convey the design principles of the language For language implementers: Define precisely what to be implemented For language programmers: Describe the language that is to be used How to describe? Natural language: ambiguous Formal ways: especially for syntax Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

5 Semantics “Colorless green ideas sleep furiously”
Chomsky created this as an example of a syntactically correct sentence that lacks any semantic value or quality other than being nonsensical. Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

6 Scanning and Parsing Lexical Structure: The structure of tokens (words) scanning phase (lexical analysis) : scanner/lexer recognize tokens from characters Syntactical Structure: The structure of programs parsing phase (syntax analysis) : parser determines the syntactic structure Eg identifier is a token that can have lexemes such a s sum and total Examples of tokens and lexemes on p 106 of statement (on previous page) index = 2 * count + 17; character stream scanner (lexical analysis) token stream parser (syntax analysis) parse tree Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

7 Tokens Tokens (words): Building blocks of programs
Reserved words (keywords): e.g., if,while,int,return Literals/constants: numeric literal: 42 string literal: "hello" Special symbols: e.g., “;”, “<=”, “+” Identifiers: e.g., x24, monthly_balance, putchar Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

8 JSON Example http://json.org/example.html
{"menu": { "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }} Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

9 Syntax example – JSON JavaScript Object Notation ( Object Array Value String Number Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

10 Syntax Example - JSON An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma). An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma). Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

11 Syntax Example - JSON A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested. Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

12 Syntax Example - JSON A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string. Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

13 Syntax Example - JSON A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used. Whitespace can be inserted between any pair of tokens. Excepting a few encoding details, that completely describes the language. Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

14 Reserved words vs. Predefined identifiers
cannot be redefined. e.g., double if; is illegal. Predefined identifiers: have initial meaning allow redefinition (not a good idea in practice) e.g., String,Object,System,Integer in Java [Demonstration] Use the Keyword example in Eclipse Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

15 Principle of Longest Substring
doif vs. do if; x12 vs. x 12 The longest possible string of characters is collected into a single token. An exception: FORTRAN DO 99 I = 1.10 (the same as DO99I=1.10) DO 99 I = 1, 10 Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

16 White Space Principle of longest substring requires that tokens are separated by white space. White space (token delimiters): Blanks, newlines, tabs ignored except that they separate tokens Free-format language: format has no effect on the program structure Most languages are free format One exception: python Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

17 Indentation in Python #error: first line indented
def perm(l): for i in range(len(l)): s = l[:i] + l[i+1:] p = perm(l[:i] + l[i+1:]) for x in p: r.append(l[i:i+1] + x) return r #error: not indented #error: unexpected indent #error: inconsistent dedent def perm(l): for i in range(len(l)): s = l[:i] + l[i+1:] p = perm(l[:i] + l[i+1:]) for x in p: r.append(l[i:i+1] + x) return r Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

18 Regular Expression A form for representing sets of strings
Description of patterns of characters Basic operations: Concatenation Repetition Selection Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

19 Regular Expression Name RE epsilon  symbol a concatenation AB
selection A | B repetition A* Shortcuts: A+ = AA* A? = A| [a-z] = (a|b|...|z) [a-z][a-z0-9]* (a|b)*aa(a|b)* [0-9]+(\.[0-9]+)? epsilon represents an empty string This is not comprehensive, refer to your RegEx tool’s manual Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

20 Tasks of a Scanner Recognizes keywords (tokens)
Recognizes special characters Recognizes identifiers, integers, reals, decimals, strings, etc. Ignores white spaces and comments Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

21 Scanner regular expression description of the tokens → (Lex or JLex)
scanner of a language Example: Figure 4.1 (page 82) Lecture 3 - Syntax, Spring 2017 21 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He 2008

22 Scanning and Parsing Lexical Structure: The structure of tokens (words) Syntactical Structure: The structure of programs regular expression grammar Eg identifier is a token that can have lexemes such a s sum and total Examples of tokens and lexemes on p 106 of statement (on previous page) index = 2 * count + 17; character stream scanner (lexical analysis) token stream parser (syntax analysis) parse tree Lecture 3 - Syntax, Spring 2017 22 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He 2008

23 Grammar Example: (1) sentence ® noun-phrase verb-phrase .
(2) noun-phrase ® article noun (3) article ® a | the (4) noun ® girl | dog (5) verb-phrase ® verb noun-phrase (6) verb ® sees | pets Figure 4.2 (page 83) Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

24 Grammar Language: the programs (character streams) allowed
Grammar rules (productions): "produce" the language left-hand side, right-hand side nonterminals (structured names): noun-phrase verb-phrase terminals (tokens): dog metasymbols: ® (“consists of”) | (choice) start symbol: the nonterminal that stands for the entire structure (sentence, program). sentence E.g., if-statement ® if (expression) statement else statement Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

25 Grammars Produce Languages
Language: the set of strings (of terminals) that can be generated from the start symbol by derivation: sentence  noun-phrase verb-phrase . (rule 1)  article noun verb-phrase . (rule 2)  the noun verb-phrase . (rule 3)  the girl verb-phrase . (rule 4)  the girl verb noun-phrase . (rule 5)  the girl sees noun-phrase . (rule 6)  the girl sees article noun . (rule 2) the girl sees a noun . (rule 3) the girl sees a dog . (rule 4) Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

26 Context-Free Grammar Context-Free Grammars (CFG) Noam Chomsky, 1950s.
Define context-free languages. Four components: terminals, nonterminals, one start symbol, productions (left-hand side: one single nonterminal) Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

27 What does “Context-Free” mean?
Left-hand side of a production is always one single nonterminal: The nonterminal is replaced by the corresponding right-hand side, no matter where the nonterminal appears. (i.e., there is no context in such replacement/derivation.) Context-sensitive grammar (context-sensitive languages) Why context-free? English example of context: Grace invited Ada over for dinner, she said thanks. Lecture 3 - Syntax, Spring 2017 27 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2007

28 Backus-Naur Form(BNF)
A meta language used to describe CFG John Backus/Peter Naur: for describing the syntax of Algol60. BNF is formal and precise Lecture 3 - Syntax, Spring 2017 28 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

29 BNF Example 1 E → ID | NUM | E*E | E/E | E+E | E-E | (E)
ID → a | b |…|z NUM → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Lecture 3 - Syntax, Spring 2017 29 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

30 BNF Example 2 S → if E then S else S | begin S L | print E L → end
E → NUM = NUM NUM → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Lecture 3 - Syntax, Spring 2017 30 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

31 Parse Tree Represents the derivation steps from start symbol to the string Given the derivations used in the parsing of an input sequence, a parse tree has the start symbol as the root the terminals of the input sequence as leafs for each production A → X1 X2 ... Xn used in a derivation, a node A with children X1 X2 ... Xn Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

32 Parse Tree Example 1 CFG: expr number Input Sequence: 234 number digit
expr → expr + expr | expr * expr | (expr) | number number → number digit |digit digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr number Input Sequence: 234 number digit number digit 4 digit 3 2 Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

33 Parse Tree Example 2 CFG: expr number digit 3 * 4 5 +
expr → expr + expr | expr * expr | (expr) | number number → number digit |digit digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 expr number digit 3 * 4 5 + Input Sequence: 3+4*5 Lecture 3 - Syntax, Spring 2017 33 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

34 Abstract Syntax Tree Parse Tree: still tedious, all terminals and nonterminals in a derivation are included in the tree. Abstract Syntax Tree: Remove ``unnecessary’’ terminals and nonterminals Still completely determine syntactic structure. number digit 2 3 4 4 3 2 Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

35 AST Example 1 expr number digit 3 * 4 5 + 3 * 4 5 +
Lecture 3 - Syntax, Spring 2017 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008

36 AST Example 2 If-statement if ( expr ) statement else If-statement
Lecture 3 - Syntax, Spring 2017 36 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Weimin He, 2008


Download ppt "CSE 3302 Programming Languages"

Similar presentations


Ads by Google