Syntax defines the grammatical rules of a programming language. There are three levels of syntax: lexical, concrete, and abstract. Lexical syntax defines tokens like literals and identifiers. Concrete syntax defines the actual representation using tokens. Abstract syntax describes a program's information without implementation details. Backus-Naur Form (BNF) uses rewriting rules to specify a grammar. BNF grammars can be ambiguous. Extended BNF simplifies recursive rules. Syntax analysis transforms a program into an abstract syntax tree used for semantic analysis and code generation.
In this slide you will explore more about how to make derivations ,design parse tree ,what is ambiguity and how to remove ambiguity ,left recursion ,left factoring .
In this slide you will explore more about how to make derivations ,design parse tree ,what is ambiguity and how to remove ambiguity ,left recursion ,left factoring .
what is Parsing
different types of parsing
what is parser and role of parser
what is top-down parsing and bottom-up parsing
what is the problem in top-down parsing
design of top-down parsing and bottom-up parsing
examples of top-down parsing and bottom-up parsing
A graph search (or traversal) technique visits every node exactly one in a systematic fashion. Two standard graph search techniques have been widely used: Depth-First Search (DFS) Breadth-First Search (BFS)
Computer Science - Programming Languages / Translators
This presentation explains the different types of translators and languages of programming such as assembler, compiler, interpreter, bytecode
what is Parsing
different types of parsing
what is parser and role of parser
what is top-down parsing and bottom-up parsing
what is the problem in top-down parsing
design of top-down parsing and bottom-up parsing
examples of top-down parsing and bottom-up parsing
A graph search (or traversal) technique visits every node exactly one in a systematic fashion. Two standard graph search techniques have been widely used: Depth-First Search (DFS) Breadth-First Search (BFS)
Computer Science - Programming Languages / Translators
This presentation explains the different types of translators and languages of programming such as assembler, compiler, interpreter, bytecode
Climbing the Abstract Syntax Tree (Bulgaria PHP 2016)James Titcumb
The new Abstract Syntax Tree (AST) in PHP 7 means the way our PHP code is being executed has changed. Understanding this new fundamental compilation step is key to understanding how our code is being run.
To demonstrate, James will show how a basic compiler works and how introducing an AST simplifies this process. We’ll look into how these magical time-warp techniques* can also be used in your code to introspect, analyse and modify code in a way that was never possible before.
After seeing this talk, you’ll have a great insight as to the wonders of an AST, and how it can be applied to both compilers and userland code.
(*actual magic or time-warp not guaranteed)
Hugging Abstract Syntax Trees: A Pythonic Love Story (OSDC 2010)Tom Lee
OSDC 2010 presentation on cool stuff that can be done with Python Abstract Syntax Trees and the compile() function.
Demo Python compiler (incomplete and hacky!) can be found here: http://github.com/thomaslee/viking-poc
Python's "batteries included" philosophy means that it comes with an astonishing amount of great stuff. On top of that, there's a vibrant world of third-party libraries that help make Python even more wonderful. We'll go on a breezy, example-filled tour through some of my favorites, from treasures in the standard library to great third-party packages that I don't think I could live without, and we'll touch on some of the fuzzier aspects of the Python culture that make it such a joy to be part of.
Using Language Oriented Programming to Execute Computations on the GPUSkills Matter
F# has a number of features that support language oriented programming (LOP) – the ability to create an abstract description of a problem then have this description executed in another environment. In this talk we’ll look at the design of an F# library that uses LOP techniques to a user execute matrix calculations either on the CPU or GPU. We’ll examine the features that F# provides to support this technique. We’ll start by taking a look at union types and active patterns, and then we’ll see how these are used by F#’s quotation system to give access to an abstract description of functions. Finally, we’ll see how these descriptions of functions can then be translated into computations the GPU understands and executed.
The French Revolution, which began in 1789, was a period of radical social and political upheaval in France. It marked the decline of absolute monarchies, the rise of secular and democratic republics, and the eventual rise of Napoleon Bonaparte. This revolutionary period is crucial in understanding the transition from feudalism to modernity in Europe.
For more information, visit-www.vavaclasses.com
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
Palestine last event orientationfvgnh .pptxRaedMohamed3
An EFL lesson about the current events in Palestine. It is intended to be for intermediate students who wish to increase their listening skills through a short lesson in power point.
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...Levi Shapiro
Letter from the Congress of the United States regarding Anti-Semitism sent June 3rd to MIT President Sally Kornbluth, MIT Corp Chair, Mark Gorenberg
Dear Dr. Kornbluth and Mr. Gorenberg,
The US House of Representatives is deeply concerned by ongoing and pervasive acts of antisemitic
harassment and intimidation at the Massachusetts Institute of Technology (MIT). Failing to act decisively to ensure a safe learning environment for all students would be a grave dereliction of your responsibilities as President of MIT and Chair of the MIT Corporation.
This Congress will not stand idly by and allow an environment hostile to Jewish students to persist. The House believes that your institution is in violation of Title VI of the Civil Rights Act, and the inability or
unwillingness to rectify this violation through action requires accountability.
Postsecondary education is a unique opportunity for students to learn and have their ideas and beliefs challenged. However, universities receiving hundreds of millions of federal funds annually have denied
students that opportunity and have been hijacked to become venues for the promotion of terrorism, antisemitic harassment and intimidation, unlawful encampments, and in some cases, assaults and riots.
The House of Representatives will not countenance the use of federal funds to indoctrinate students into hateful, antisemitic, anti-American supporters of terrorism. Investigations into campus antisemitism by the Committee on Education and the Workforce and the Committee on Ways and Means have been expanded into a Congress-wide probe across all relevant jurisdictions to address this national crisis. The undersigned Committees will conduct oversight into the use of federal funds at MIT and its learning environment under authorities granted to each Committee.
• The Committee on Education and the Workforce has been investigating your institution since December 7, 2023. The Committee has broad jurisdiction over postsecondary education, including its compliance with Title VI of the Civil Rights Act, campus safety concerns over disruptions to the learning environment, and the awarding of federal student aid under the Higher Education Act.
• The Committee on Oversight and Accountability is investigating the sources of funding and other support flowing to groups espousing pro-Hamas propaganda and engaged in antisemitic harassment and intimidation of students. The Committee on Oversight and Accountability is the principal oversight committee of the US House of Representatives and has broad authority to investigate “any matter” at “any time” under House Rule X.
• The Committee on Ways and Means has been investigating several universities since November 15, 2023, when the Committee held a hearing entitled From Ivory Towers to Dark Corners: Investigating the Nexus Between Antisemitism, Tax-Exempt Universities, and Terror Financing. The Committee followed the hearing with letters to those institutions on January 10, 202
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
This presentation provides a briefing on how to upload submissions and documents in Google Classroom. It was prepared as part of an orientation for new Sainik School in-service teacher trainees. As a training officer, my goal is to ensure that you are comfortable and proficient with this essential tool for managing assignments and fostering student engagement.
A Strategic Approach: GenAI in EducationPeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
2. Syntax
• Syntax defines what is grammatically valid in a programming
language
– Set of grammatical rules
– E.g. in English, a sentence cannot begin with a period
– Must be formal and exact or there will be ambiguity in a
programming language
• We will study three levels of syntax
– Lexical
• Defines the rules for tokens: literals, identifiers, etc.
– Concrete
• Actual representation scheme down to every semicolon, i.e. every lexical
token
– Abstract
• Description of a program’s information without worrying about specific
details such as where the parentheses or semicolons go
3. BNF Grammar
• BNF = Backus-Naur Form to specify a grammar
– Equivalent to a context free grammar
• Set of rewriting rules (a rule that can be applied multiple
times) defined on a set of nonterminal symbols, a set of
terminal symbols, and a start symbol
– Terminals, ∑ : Basic alphabet from which programs are
constructed. E.g., letters, digits, or keywords such as “int”,
“main”, “{“, “}”
– Nonterminals, N : Identify grammatical categories
– Start Symbol: One of the nonterminals which identifies the
principal category. E.g., “Sentence” for english, “Program” for a
programming language
4. Rewriting Rules
• Rewriting Rules, ρ
– Written using the symbols and |
| is a separator for alternative definitions, i.e. “OR”
is used to define a rule, i.e. “IS”
– Format
• LHS RHS1 | RHS2 | RHS3 | …
• LHS is a single nonterminal
• RHS is any sequence of terminals and nonterminals
5. Sample Grammars
• Grammar for subset of English
Sentence Noun Verb
Noun Jack | Jill
Verb eats | bites
• Grammar for a digit
Digit 0 | 1 | 2 | 3 | 4 | 5 | 6 |7 |8 |9
• Grammar for signed integers
SignedInteger Sign Integer
Sign + | -
Integer Digit | Digit Integer
• Grammar for subset of Java
Assignment Variable = Expression
Expression Variable | Variable + Variable | Variable – Variable
Variable X | Y
6. Derivation
• Process of parsing data using a grammar
– Apply rewrite rules to non-terminals on the RHS of an existing
rule
– To match, the derivation must terminate and be composed of
terminals only
• Example
Digit 0 | 1 | 2 | 3 | 4 | 5 | 6 |7 |8 |9
Integer Digit | Digit Integer
– Is 352 an Integer?
Integer → Digit Integer → 3 Integer →
3 Digit Integer → 3 5 Integer →
3 5 Digit → 3 5 2
Intermediate formats are called sentential forms
This was called a Leftmost Derivation since we replaced the
leftmost nonterminal symbol each time (could also do Rightmost)
7. Derivation and Parse Trees
• The derivation can be
visualized as a parse
tree
Integer
Digit Integer
Digit Integer
3
5 Digit
2
9. BNF and Languages
• The language defined by a BNF grammar is the set of all
strings that can be derived
– Language can be infinite, e.g. case of integers
• A language is ambiguous if it permits a string to be parsed
into two separate parse trees
– Generally want to avoid ambiguous grammars
– Example:
• Expr Integer | Expr + Expr | Expr * Expr | Expr - Expr
• Parse: 3*4+1
– Expr * Expr → Integer * Expr →
3 * Expr → 3 * Expr+Expr → … 3 * 4 + 1
– Expr + Expr → Expr + Integer → Expr + 1
Expr * Expr +1 → … 3 * 4 + 1
13. How to fix ambiguity?
• Use explicit grammar without ambiguity
– E.g., add an “ENDIF” for every “IF”
– Java makes a separate category for if-else vs. if:
IfThenStatement If (Expr) Statement
IfThenElseStatement If (Expr) StatementNoShortIf else
Statement
StatementNoShortIf contains everything except
IfThenStatement, so the else always goes with the
IfThenElse statement not the IfThenStatement
• Use precedence on symbols
14. Alternative to BNF
• The use of regular expressions is an alternate
way to express a language
15. Regex to EBNF
• The book uses some deviations from “standard”
regular expressions in Extended Backus Naur
Format (defined in a few slides)
{ M } means zero or more occurrences of M
( M | N) means one of M or N must be chosen
[M] means M is optional
Use “{“ to mean the literal { not the regex {
16. RegEx Examples
• Booleans
– “true” | “false”
• Integers
– (0-9)+
• Identifiers
– (a-zA-Z){a-zA-Z0-9}
• Comments (letters/space only)
– “//”{a-zA-Z }(“r” | “n” | “rn”)
• Regular expressions seem pretty powerful
– Can you write one for the language anbn? (i.e. n a’s followed by n
b’s)
17. Extended BNF
• EBNF – variation of BNF that simplifies
specification of recursive rules using regular
expressions in the RHS of the rule
• Example:
– BNF rule
Expr Term | Expr + Term | Expr – Term
Term Factor | Term * Factor | Term / Factor
– EBNF equivalent
Expr Term { [+|-] Term }
Term Factor { [* | / ] Factor }
• EBNF tends to be shorter and easier to read
18. EBNF
• Consider:
Expr Term{ (+|-) Term }
Term Factor { (* | / ) Factor }
Factor Identifier | Literal | (Expr)
Parse for X+2*Y
19. BNF and Lexical Analysis
• Lexicon of a programming language – set of all
nonterminals from which programs are written
• Nonterminals – referred to as tokens
– Each token is described by its type (e.g. identifier,
expression) and its value (the string it represents)
– Skipping whitespace or comments
or punctuation
20. Categories of Lexical Tokens
• Identifiers
• Literals
Includes Integers, true, false, floats, chars
• Keywords
bool char else false float if int main true while
• Operators
= || && == != < <= > >= + - * / % ! [ ]
• Punctuation
;.{}()
Issues to consider: Ignoring comments, role of whitespace,
distinguising the < operator from <=, distinguishing
identifiers from keywords like “if”
21. A Simple Lexical Syntax for a Small
Language, Clite
Primary → Identifier [ "["Expression"]" ] | Literal | "("Expression")"
| Type "("Expression")"
Identifier → Letter { Letter | Digit }
Letter → a | b | … | z | A | B | … Z
Digit → 0 | 1 | 2 | … | 9
Literal → Integer | Boolean | Float | Char
Integer → Digit { Digit }
Boolean → true | false
Float → Integer . Integer
Char → ‘ ASCIICHAR ‘
22. Major Stages in Compilation
• Lexical Analysis
– Translates source into a stream of Tokens, everything else
discarded
• Syntactic Analysis
– Parses tokens, detects syntax errors, develops abstract
representation
• Semantic Analysis
– Analyze the parse for semantic consistency, transform into a
format the architecture can efficiently run on
• Code Generation
– Use results of abstract representation as a basis for generating
executable machine code
23. Lexical Analysis & Compiling
Process
Difficulties: 1 to many mapping from HL source to machine code
Translation must be correct
Translation should be efficient
24. Lexical Analysis of Clite
• Lexical Analysis – transforms a program
into tokens (type, value). The rest is tossed.
• Example Clite program:
// Simple Program
int main() {
int x;
x = 3;
}
Result of Lexical Analysis:
25. Lexical Analysis (2)
Result of Lexical Analysis:
1 Type: Int Value: int
2 Type: Main Value: main
3 Type: LeftParen Value: ( // Simple Program
4 Type: RightParen Value: ) int main() {
5 Type: LeftBrace Value: { int x;
6 Type: Int Value: int x = 3;
7 Type: Identifier Value: x }
8 Type: Semicolon Value: ;
9 Type: Identifier Value: x
10 Type: Assign Value: =
11 Type: IntLiteral Value: 3
12 Type: Semicolon Value: ;
13 Type: RightBrace Value: }
14 Type: Eof Value: <<EOF>>
26. Lexical Analysis of Clite in Java
public class TokenTester {
public static void main (String[] args) {
Lexer lex = new Lexer (args[0]);
Token t;
int i = 1;
do
{
t = lex.next();
System.out.println(i+" Type: "+t.type()
+"tValue: "+t.value());
i++;
} while (t != Token.eofTok);
}
}
The source code for how the Lexer and Token classes are arranged
is the topic of chapter 3
27. Lexical to Concrete
• From the stream of tokens generated by our
lexical analyzer we can now parse them
using a concrete syntax
28. Concrete EBNF Syntax for Clite
Program → int main ( ) { Declarations Statements }
Declarations → { Declaration }
Declaration → Type Identifier [ "["Integer"]" ] { , Identifier ["["Integer"]"] };
Type → int | bool | float | char
Statements →{ Statement }
Statement → ; | Block | Assignment | IfStatement | WhileStatement
Block → { Statements }
Assignment → Identifier ["["Expression"]" ] = Expression ;
IfStatement → if "(" Expression ")" Statement [ else Statement ]
WhileStatement → while "("Expression")" Statement
Concrete Syntax;
Higher than lexical
syntax!
30. Syntax Diagram
• Alternate way to specify a language
• Popularized with Pascal
• Not any more powerful than BNF, EBNF, or regular
expressions
31. Linking Syntax and Semantics
• What we’ve described so far has been
concrete syntax
– Defines all parts of the language above the
lexical level
• Assignments, loops, functions, definitions, etc.
• Uses BNF or variant to describe the language
• An abstract syntax links the concrete syntax
to the semantic level
32. Abstract Syntax
• Defines essential syntactic elements without
describing how they are concretely
constructed
• Consider the following Pascal and C loops
Pascal C
while i<n do begin while (i<n) {
i:=i+1 i=i+1;
end }
Small differences in concrete syntax; identical abstract construct
33. Abstract Syntax Format
• Defined using rules of the form
– LHS = RHS
• LHS is the name of an abstract syntactic class
• RHS is a list of essential components that define the
class
– Similar to defining a variable. Data type or abstract
syntactic class, and name
– Components are separated by ;
• Recursion naturally occurs among the
definitions as with BNF
34. Abstract Syntax Example
• Loop
Loop = Expression test ; Statement body
– The abstract class Loop has two components, a test which is a
member of the abstract class Expression, and a body which is a
member of an abstract class Statement
• Nice by-product: If parsing abstract syntax in Java, it
makes sense to actually define a class for each abstract
syntactic class, e.g.
class Loop extends Statement {
Expression test;
Statement body;
}
35. Abstract Syntax of Clite
Program = Declarations decpart; Statements body;
Declarations = Declaration*
Declaration = VariableDecl | ArrayDecl
VariableDecl = Variable v; Type t
ArrayDecl = Variable v; Type t; Integer size
Type = int | bool | float | char
Statements = Statement*
Statement = Skip | Block | Assignment |
Conditional | Loop
Skip =
Block = Statements
Conditional = Expression test;
Statement thenbranch, elsebranch
Loop = Expression test; Statement body
Assignment = VariableRef target; Expression source
Expression = VariableRef | Value | Binary | Unary
37. Java AbstractSyntax for Clite
class Loop extends Statement {
Expression test;
Statement body;
}
Class Assignment extends Statement {
// Assignment = Variable target; Expression source
Variable target;
Expression source;
}
…
Much more… see the file (when available)
38. Abstract Syntax Tree
• Just as we can build a parse tree from a BNF grammar, we
can build an abstract syntax tree from an abstract syntax
• Example for: x+2*y
Expression = Variable | Value | Binary
Binary = Operator op ; Expression term1, term2
Binary node
Expr
41. Concrete and Abstract Syntax
• Aren’t the two redundant?
– A little bit
• The concrete syntax tells the programmer exactly
what to write to have a valid program
• The abstract syntax allows valid programs in two
different languages to share common abstract
representations
– It is closer to semantics
– We need both!
42. What’s coming up?
• Semantic analysis
– Do the types match? What does this mean?
char a=‘c’;
int sum=0;
sum = sum = a;
• Can associate machine code with the abstract
parse
– Code generation
– Code optimization