Dr. Philip Cannata 10 Java (Object Java (Object Oriented) Oriented) ASP ASP RDF (Horn Clause RDF (Horn Clause Deduction, Semantic Deduction, Semantic Web) Web) Relation Relation Jython in Jython in Java Java This Course This Course High Level High Level Languages Languages
38
Embed
Dr. Philip Cannata 1 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython in Java This Course High Level Languages.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
x a character x \x an escaped character, e.g., \n{ name } a reference to a nameM | N M or NM N M followed by NM* zero or more occurrences of MM+ One or more occurrences of MM? Zero or one occurrence of M[aeiou] the set of vowels[0-9] the set of digits. any single character
Dr. Philip Cannata 8
Regular Expressions
Dr. Philip Cannata 9
Regular Expressions
Dr. Philip Cannata 10
(S, a2i$) ├ (I, 2i$)
├ (I, i$)
├ (I, $)
├ (F, )
Thus: (S, a2i$) ├* (F, )
Finite State Automaton for Identifiers
Dr. Philip Cannata 11
•
Deterministic Finite State Automaton Examples
Dr. Philip Cannata 12
Production:
α → β
α Nonterminal
β (Nonterminal Terminal)*
ie, lefthand side is a single nonterminal, and righthand side is a string of nonterminals and/or terminals (possibly empty).
Context-Free Grammar
Dr. Philip Cannata 13
Production:
α → β |α| ≤ |β|
α, β (Nonterminal Terminal)*
ie, lefthand side can be composed of strings of terminals and nonterminals, however, the number of items on the left must be smaller than the number of items on the right.
Context-Sensitive Grammar
Dr. Philip Cannata 14
• The syntax of a programming language is a precise description of all its grammatically correct programs.
• Precise syntax was first used with Algol 60, and has been
used ever since.
• Three levels:– Lexical syntax - all the basic symbols of the language
statements and programs.– Abstract syntax - internal representation of the program,
favoring content over form.
Syntax
Dr. Philip Cannata 15
GrammarsGrammars: Metalanguages used to define the concrete syntax of a language.
Backus Normal Form – Backus Naur Form (BNF)• Stylized version of a context-free grammar (cf. Chomsky hierarchy)
• First used to define syntax of Algol 60
• Now used to define syntax of most major languages Production: α → β α Nonterminal β (Nonterminal Terminal)*ie, lefthand side is a single nonterminal, and β is a string of nonterminals and/or terminals (possibly empty).
• ExampleInteger Digit | Integer Digit
Digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Dr. Philip Cannata 16
Extended BNF (EBNF)
Additional metacharacters{ } a series of zero or more( ) must pick one from a list[ ] pick none or one from a list
ExampleExpression -> Term { ( + | - ) Term }IfStatement -> if ( Expression ) Statement [ else Statement ]
EBNF is no more powerful than BNF, but its production rules are often simpler and clearer.
Javacc EBNF
( … )* a series of zero or more( … )+ a series of one or more[ … ] optional
Dr. Philip Cannata 17
For more details, see Chapter 2 of“Programming Language Pragmatics, Third Edition (Paperback)”Michael L. Scott (Author)
Dr. Philip Cannata 18
Internal Parse Tree
Abstract Syntax
int main ()
{
return 0 ;
}
Program (abstract syntax): Function = main; Return type = int params = Block: Return: Variable: return#main, LOCAL addr=0 IntValue: 0
Expr Expr + Term | Expr – Term | TermTerm 0 | ... | 9 | ( Expr )
Parse of 5 - 4 + 3
Dr. Philip Cannata 22
• A grammar can be used to define associativity and precedence among the operators in an expression.
E.g., + and - are left-associative operators in mathematics;
* and / have higher precedence than + and - .
• Consider the following grammar:Expr -> Expr + Term | Expr – Term | Term
Term -> Term * Factor | Term / Factor | Term % Factor | Factor
Factor -> Primary ** Factor | Primary
Primary -> 0 | ... | 9 | ( Expr )
Associativity and Precedence
Dr. Philip Cannata 23
Associativity and Precedence
Parse of 4**2**3 + 5 * 6 + 7
Dr. Philip Cannata 24
Precedence Associativity Operators3 right **2 left * / %1 left + -
Note: These relationships are shown by the structure of the parse tree: highest precedence at the bottom, and left-associativity on the left at each level.
Associativity and Precedence
Dr. Philip Cannata 25
• A grammar is ambiguous if one of its strings has two or more diffferent parse trees.