Download presentation
Presentation is loading. Please wait.
Published byEdmund McGee Modified over 6 years ago
1
What does it mean? Notes from Robert Sebesta Programming Languages
Syntax and Semantics What does it mean? Notes from Robert Sebesta Programming Languages
2
Syntax Rules for the language In English some syntax rules are:
Sentence starts with capital letter Sentence ends with ‘.’, ‘?’, ‘!’ Names are capitalized Spaces between words
3
Syntax In programming, is the form of expressions, statements, and program units if(a==b) { … } else { }
4
Semantics Is meaning. In the preceding statement the meaning is that the computer must evaluate a conditional and make a choice based on it.
5
Lexemes A language is just a set of strings of some alphabet
Lexemes – smallest units usable in a language (i.e. letters, numbers, symbols) const, for, while, if, a, b, int, 1 Programs are more a string of lexemes than a string of characters
6
Tokens Lexemes are broken into categories or groups called tokens
A token might be Identifiers (a variable or function name for instance) Literals (numbers for instance) Operators (equal sign, addition sign, etc.)
7
Tokens (example) index = 2 * count + 17; Lexemes Tokens index
identifier = Equal_sign 2 Int_literal * Mult_op count + Plus_op 17 ; semicolon
8
Formally Defining Languages: Language Recognizers
If we have a language L that uses an alphabet Σ And we have a mechanism R that could read strings of characters from Σ R could accept a string only if it is in L Example is the syntax analysis part of a compiler. Determines if given programs are syntactically correct.
9
Formally Defining Languages: Language Generators
A device that can be used to generate the sentences of a language. Imagine a button that produces a sentence from the language each time it is pushed. May be more useful to a programmer; can produce sentences we can compare our code to to determine correct syntax without having to feed it to a compiler and hope for the best
10
Context-Free Grammars
Developed by linguist Noam Chomsky Is a set of rules whereby possible strings are formed by replacements All possible strings in the language can be produced through replacements
11
Backus-Naur Form (BNF)
Developed at the same time as Chomsky’s ideas, but in completely different research efforts Was created for the description of ALGOL 60 Nearly identical to Chomsky’s work
12
How do they work? Both use metalanguage to describe an abstraction
<assign> -> <var> = <expression> Text on left-hand side is the abstraction being defined Text on right-hand side is the definition Together is called a rule or a production One (of MANY) example sentences this describes is: x = y + z
13
Terminology Nonterminal symbols – the abstractions in the BNF description Terminal symbols – lexemes and tokens
14
Example <if_stmt> -> if ( <logic_expr> ) <stmt> | if ( <logic_expr> ) <stmt> else <stmt>
15
Rules can be recursive…
<ident_list> -> <identifier> | <identifier>, <ident_list> This one says that a list is either a single token or an identifier followed by a comma and another <ident_list>
16
Begins with a start symbol
We may call it whatever we wish, but useful names might be <program> <statements> <instruction_list> Etc…
17
Example (from book) A = B * (A + C)
18
Leftmost derivation <assign> => <id> = <expr> => A = <expr> => A = <id> * <expr> => A = B * <expr> => A = B * ( <expr> ) => A = B * ( <id> + <expr> ) => A = B * ( A + <expr> ) => A = B * ( A + <id> ) => A = B * ( A + C )
19
In-class (no more than 2 or 3 mins…)
A = ( B + A ) * A + B
20
Because it isn’t valid! (no rule for <expr> * <expr>
21
This one is though! C = A * ( A + B * C )
22
Parse Trees Grammars describe hierarchy well.
Therefore we can view productions visually.
24
Parse Trees Every internal node is a non-terminal symbol
Every leaf is a terminal
25
Ambiguity When there are two or more parse trees that are valid.
NOT GOOD!
26
For instance
27
A = B + C * A
28
Semantics
29
Order of Operations * and + have different meanings. The previous grammar didn’t deal with that. * should have a higher precedence and therefore be lower in the parse tree We must rewrite the grammar. We can do this by using additional non- terminals and new rules
30
Unambiguous Grammar
31
Sentence Generated
32
Parse Tree
33
Associativity Two operators of the same level of precedence
* and / Or two of the same operators on one line + and + Not always an issue but can be. 10 / 5 / 5 = 10 / (5 / 5) or (10 / 5 ) / 5 ? Must have a rule in place. i.e. left to right associativity
34
Lexical Analysis and Syntax Analysis
35
Lexical Analyzer ”Front-end” of the syntax analyzer
Is a pattern matcher (regexes!) Collects characters into ”lexemes” We group lexemes into tokens
36
Lexical Analysis
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.