The document discusses the syntax analysis phase of compilers. It introduces tokenizing, which breaks source code into individual tokens, and parsing, which checks that the tokens conform to the language's grammar. It provides examples of Jack code being tokenized and parsed. The parsing process involves matching tokens to rules in a grammar using a recursive descent parser. The output of syntax analysis is typically a parse tree that represents the structural relationship between tokens. The document outlines proposed implementations for a Jack tokenizer and parser that would perform syntax analysis on Jack code and output an XML representation of the parse tree.
interfacing matlab with embedded systemsRaghav Shetty
This Book is all about Interfacing Embedded System with Matlab. This book guides the beginners for creating GUI , Modeling with SimuLink & Interfacing Arduino , Raspberry Pi , BeagleBone with Embedded System. This Book is NOT FOR SALE , Only knowledge base for Open Source Community
This presentation comes with many additional notes (pdf): http://de.slideshare.net/nicolayludwig/6-collections-algorithms-38614039
- Producing Collection Objects
- Immutable Collections and defense Copies
- Empty Collections
- Tuples as pseudo Collections
- Filling Collections with structured Data
- Collections abstracting Data Processing: Stack and Queue
- Kung Fu beyond Collections: .Net's LINQ and Java's Streams
interfacing matlab with embedded systemsRaghav Shetty
This Book is all about Interfacing Embedded System with Matlab. This book guides the beginners for creating GUI , Modeling with SimuLink & Interfacing Arduino , Raspberry Pi , BeagleBone with Embedded System. This Book is NOT FOR SALE , Only knowledge base for Open Source Community
This presentation comes with many additional notes (pdf): http://de.slideshare.net/nicolayludwig/6-collections-algorithms-38614039
- Producing Collection Objects
- Immutable Collections and defense Copies
- Empty Collections
- Tuples as pseudo Collections
- Filling Collections with structured Data
- Collections abstracting Data Processing: Stack and Queue
- Kung Fu beyond Collections: .Net's LINQ and Java's Streams
Programming Languages Implementation and Design. .docxaryan532920
/**
* Programming Languages: Implementation and Design.
*
* A Simple Compiler Adapted from Sebesta (2010) by Josh Dehlinger further modified by Adam Conover
* (2012-2015)
*
* A simple compiler used for the simple English grammar in Section 2.2 of Adam Brooks Weber's
* "Modern Programming Languages" book. Parts of this code was adapted from Robert Sebesta's
* "Concepts of Programming Languages".
*
* This compiler assumes that the source file containing the sentences to parse is provided as the
* first runtime argument. Within the source file, the compiler assumes that each sentence to parse
* is provided on its own line.
*
* NOTE: A "real" compiler would more likely treat an entire file as a single stream of input,
* rather than each line being an independent input stream.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Compiler {
/**
* It is assumed that the first argument provided is the name of the source file that is to be
* "compiled".
*/
public static void main(String[] args) throws IOException {
//args = new String[]{"<some hard coded path for testing>"};
//args = new String[]{"D:\\Version_Controlled\\_SVN_\\Newton\\COS420\\Java\\ParserSample\\input.txt"};
if (args.length < 1) {
System.out.println("Need a filename!");
} else {
// Java 7 "try-with-resource" to create the file input buffer.
try (BufferedReader br = new BufferedReader(new FileReader(args[0]))) {
// Create the new lexer.
LexicalAnalyzer lexer = new LexicalAnalyzer();
// Start lexing and parsing.
processFile(lexer, br);
}
}
}
/**
* Reads each line of the input file and invokes the lexer and parser for each.
*/
static void processFile(LexicalAnalyzer lexer, BufferedReader br) throws IOException {
String sourceLine;
// Read each line in the source file to be compiled as a unique sentence
// to check against the grammar.
while ((sourceLine = br.readLine()) != null) {
// Ignore empty lines and comments.
if (sourceLine.trim().length() <= 0) {
continue;
}
if (sourceLine.trim().startsWith("#")) {
System.out.println("Comment: " + sourceLine.substring(1).trim());
continue;
}
// Create a new syntax analyzer over the provided lexer.
SyntaxAnalyzer parser = new SyntaxAnalyzer(lexer);
// Parse the given sentence against the given grammar. We assume that the
// sentence, <S>, production is the start state.
try {
// Start the lexer...
lexer.start(sourceLine);
// Start the parser...
parser ...
1-Information sharing
2-Computation speedup
3-Modularity
4-Convenience
5-allows exchanged data and informations
Two IPC Models
1. Shared memory- is an OS provided abstraction which allows a memory region to be simultaneously accessed by multiple programs with an intent to provide communication among them. One process will create an area in RAM which other processes can accessed
2. Message passing - is a form of communication used in interprocess communication. Communication is made by the sending of messages to recipients. Each process should be able to name the other processes. The producer typically uses send() system call to send messages, and the consumer uses receive()system call to receive messages
Shared memory
Faster than message passing
After establishing shared memory, treated as routine memory accesses
Message passing
Useful for exchanging smaller amounts of data
Easy to implement, but more time-consuming task of kernel intervention
Bounded-Buffer Problem Producer Process
do {
...
produce an item in nextp
...
wait(empty);
wait(mutex);
...
add nextp to buffer
...
signal(mutex);
signal(full);
} while (true);
Bounded-Buffer Problem Consumer Process
do {
wait(full);
wait(mutex);
...
remove an item from buffer to nextc
...
signal(mutex);
signal(empty);
...
consume the item in nextc
...
} while (true);
client-server model, the client sends out requests to the server, and the server does some processing with the request(s) received, and returns a reply (or replies)to the client.
Since Socket can be described as end-points for communication. we could imagine the client and server hosts being connected by a pipe through which data-flow takes place.
1-sockets use a client-server while Server waits for incoming client requests by listening to a specified port.
2-After receiving a request, the server accepts a connection from the client socket to complete the connection
3-then Remote procedure call (RPC) abstracts procedure call mechanism for use between systems with network connections
4-and pipes acts as a conduit allowing two processes to communicate
A process is different than a program
- Program is static code and static data
- Process is Dynamic instance of code and data
-Program becomes process when executable file loaded into memory
No one-to-one mapping between programs and processes
-can have multiple processes of the same program
-one program can invoke multiple process
Execution of program started via GUI mouse clicks and command line entry of its name
The process state transition
As a process executes, The process is being created, then The process is waiting to be assigned to a processor therefore, Instructions are being executed then The process is waiting for some event to occur,thereafter The process has finished exec ...
Article link httpiveybusinessjournal.compublicationmanaging-.docxfredharris32
Article link: http://iveybusinessjournal.com/publication/managing-global-risk-to-seize-competitive-advantage/
Requirements: Write one summary and study note both no longer than one pages should include all point of article. Then do a PPT and write a presenting paper only for 5 minutes.
Groups of students will create and offer two MS PowerPoint presentation summarizing the main points of one of the readings for this course along with a one page handout for the students in the class. The aim of the presentations and the handouts is to provide the audience with the main ideas of the article and study notes. Groups will bring to class enough copies of the handout for each student in the class. The handout should list the name of the author, the title of the article, the title of the journal, and the publication date and page numbers along with a summary of its main points. Please do not exceed one page for this material.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* Read a .dat file and reverse it.
*/
public class Reverse {
public static void main(String[]args) {
if (args.length != 3) {
System.err.println(" Incorrect number of arguments");
System.err.println(" Usage: ");
System.err.
println("\tjava Reverse <stack type> <input file> <output file>");
System.exit(1);
}
boolean useList = true;
if (args[0].compareTo("list")==0)
useList = true;
else if (args[0].compareTo("array")==0)
useList = false;
else {
System.err.println("\tSaw "+args[0]+" instead of list or array as first argument");
System.exit(1);
}
try {
//
// Set up the input file to read, and the output file to write to
//
BufferedReader fileIn =
new BufferedReader(new FileReader(args[1]));
PrintWriter fileOut =
new PrintWriter(new
BufferedWriter(new FileWriter(args[2])));
//
// Read the first line of the .dat file to get sample rate.
// We want to store the sample rate value in a variable,
// but we can ignore the "; Sample Rate" part of the line.
// Step through the first line one token (word) at a time
// using the StringTokenizer. The fourth token is the one
// we want (the sample rate).
//
StringTokenizer str;
String oneLine;
int sampleRate;
String strJunk;
oneLine = fileIn.readLine();
str = new StringTokenizer(oneLine);
strJunk = str.nextToken(); // Read in semicolon
strJunk = str.nextToken(); // Read in "Sample"
strJunk = str.nextToken(); // Read in "Rate"
// ...
The program reads data from two files, itemsList-0x.txt and .docxoscars29
The program reads data from two files,
itemsList-0x.txt
and
inventoryList-0x.txt
. File extensions on Linux may be arbitrary–i.e., these files could have been named with
.dat
as the extensions.
The first file,
itemsList-0x.txt
, lists all possible items. Each line represents one item in the form
id name
.
Example 1: Sample itemsList-0x.txt
0 Air 1 HP Potion 2 MP Potion 5 Iron Ore 3 Bow Tie 4 Dirt 6 Diamond Ore 7 Iron Ingot 8 Diamond 9 Diamond Block
The second file,
inventoryList-0x.txt
, lists each individual inventory–or storage chest–followed by a list of items.
Example 2: Sample inventoryList-0x.txt
# 5
- 1 10 - 2 5 - 3 2 # 6
- 4 3 - 5 27 - 6 44 - 7 55 - 8 1 - 9 4 - 4 3 # 2
- 2 5 - 9 4 - 8 1 - 5 2 - 10 5
Each line preceded by
#
denotes the start of a new inventory. Each line preceded by
-
denotes an item. The program creates a new inventory each time a
#
is encountered.
When a
-
is encountered, a stack of items, ItemStack, is created. The
ItemStack
is placed in the
Inventory
based on the following rules:
If the Inventory is empty, store the ItemStack, and
return true
.
If the Inventory is not empty, examine the Inventory.
If a matching ItemStack is found, merge the two ItemStacks and
return true
.
If no matching ItemStack is found, store the new ItemStack and
return true
.
If the Inventory is full,
return false
.
Through the magic of abstraction, this is not one function, but four (4) functions in total. Yes, it does seem unnecessary at first. However, each function does one thing and only one thing. This is an exercise in understanding the thought process behind abstraction, interfaces, and the
S
/
O
in
S.O.L.I.D
(with some C++ code) in a multi-ADT program.
Most of your time will be spent on understanding the abstractions (and interfaces) as opposed to spamming cobblestone blocks… I mean C++ code.
3.2 Output
The output consists of three reports written to standard output, one after the other.
A report listing items that were stored or discarded.
A report listing all valid items.
Finally, a detailed report is printed. listing data for each inventory:
Maximum Capacity–i.e., total slots.
Utilized Capacity–i.e., occupied slots
Listing of all items.
If the program is run with the provided input files, the following output should be generated…
Example 3: Sample Output
Processing Log: Stored (10) HP Potion Stored ( 5) MP Potion Stored ( 2) Bow Tie Stored ( 3) Dirt Stored (27) Iron Ore Stored (44) Diamond Ore Stored (55) Iron Ingot Stored ( 1) Diamond Stored ( 4) Diamond Block Stored ( 3) Dirt Stored ( 5) MP Potion Stored ( 4) Diamond Block Discarded ( 1) Diamond Discarded ( 2) Iron Ore Item List: 0 Air 1 HP Potion 2 MP Potion 3 Bow Tie 4 Dirt 5 Iron Ore 6 Diamond Ore 7 Iron Ingot 8 Diamond 9 Diamond Block Storage Summary: -Used 3 of 5 slots (10) HP Potion ( 5) MP Potion ( 2) Bow Tie -Used 6 of 6 slots ( 6) Dirt.
These questions will be a bit advanced level 2sadhana312471
These questions will be a bit advanced(Intermediate) in terms of Python interview.
This is the continuity of Nail the Python Interview Questions.
The fields that these questions will help you in are:
• Python Developer
• Data Analyst
• Research Analyst
• Data Scientist
A workshop hosted by the South African Journal of Science aimed at postgraduate students and early career researchers with little or no experience in writing and publishing journal articles.
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
A review of the growth of the Israel Genealogy Research Association Database Collection for the last 12 months. Our collection is now passed the 3 million mark and still growing. See which archives have contributed the most. See the different types of records we have, and which years have had records added. You can also see what we have for the future.
Francesca Gottschalk - How can education support child empowerment.pptxEduSkills OECD
Francesca Gottschalk from the OECD’s Centre for Educational Research and Innovation presents at the Ask an Expert Webinar: How can education support child empowerment?
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Dr. Vinod Kumar Kanvaria
Exploiting Artificial Intelligence for Empowering Researchers and Faculty,
International FDP on Fundamentals of Research in Social Sciences
at Integral University, Lucknow, 06.06.2024
By Dr. Vinod Kumar Kanvaria
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.
Macroeconomics- Movie Location
This will be used as part of your Personal Professional Portfolio once graded.
Objective:
Prepare a presentation or a paper using research, basic comparative analysis, data organization and application of economic information. You will make an informed assessment of an economic climate outside of the United States to accomplish an entertainment industry objective.
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.
Executive Directors Chat Leveraging AI for Diversity, Equity, and InclusionTechSoup
Let’s explore the intersection of technology and equity in the final session of our DEI series. Discover how AI tools, like ChatGPT, can be used to support and enhance your nonprofit's DEI initiatives. Participants will gain insights into practical AI applications and get tips for leveraging technology to advance their DEI goals.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
How libraries can support authors with open access requirements for UKRI fund...
Lecture 10 compiler i
1. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 1
www.nand2tetris.org
Building a Modern Computer From First Principles
Compiler I: Syntax Analysis
2. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 2
Course map
Assembler
Chapter 6
H.L. Language
&
Operating Sys.
abstract interface
Compiler
Chapters 10 - 11
VM Translator
Chapters 7 - 8
Computer
Architecture
Chapters 4 - 5
Gate Logic
Chapters 1 - 3 Electrical
Engineering
Physics
Virtual
Machine
abstract interface
Software
hierarchy
Assembly
Language
abstract interface
Hardware
hierarchy
Machine
Language
abstract interface
Hardware
Platform
abstract interface
Chips &
Logic Gates
abstract interface
Human
Thought
Abstract design
Chapters 9, 12
3. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 3
Motivation: Why study about compilers?
Because Compilers …
Are an essential part of applied computer science
Are very relevant to computational linguistics
Are implemented using classical programming techniques
Employ important software engineering principles
Train you in developing software for transforming one structure to
another (programs, files, transactions, …)
Train you to think in terms of ”description languages”.
4. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 4
The big picture
. . .
RISC
machine
other digital platforms, each equipped
with its VM implementation
RISC
machine
language
Hack
computer
Hack
machine
language
CISC
machine
language
CISC
machine
. . .
written in
a high-level
language
Any
computer
. . .
HW
lectures
(Projects
1-6)
Intermediate code
VM
implementation
over CISC
platforms
VM imp.
over RISC
platforms
VM imp.
over the Hack
platform
VM
emulator
VM
lectures
(Projects
7-8)
Some Other
language
Jack
language
Some
compiler Some Other
compiler
Jack
compiler
. . .Some
language
. . .
Compiler
lectures
(Projects
10,11)
Modern compilers
are two-tiered:
Front-end:
from high-level
language to some
intermediate
language
Back-end:
from the
intermediate
language to
binary code.
5. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 5
Compiler architecture (front end)
. . .
Intermediate code
RISC
machine
language
Hack
machine
language
CISC
machine
language
. . .
written in
a high-level
language
. . .
VM
implementation
over CISC
platforms
VM imp.
over RISC
platforms
VM imp.
over the Hack
platform
VM
emulator
Some Other
language
Jack
language
Some
compiler Some Other
compiler
Jack
compiler
. . .Some
language
. . .
Syntax analysis: understanding the semantics implied by the source code
Code generation: reconstructing the semantics using the syntax of the
target code.
Tokenizing: creating a stream of “atoms”
Parsing: matching the atom stream with the language grammar
XML output = one way to demonstrate that the syntax analyzer works
(Chapter 11)Jack
Program
Toke-
nizer
Parser
Code
Gene
-ration
Syntax Analyzer
Jack Compiler
VM
code
XML
code
(Chapter 10)
(source) (target)
6. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 6
Tokenizing / Lexical analysis
Remove white space
Construct a token list (language atoms)
Things to worry about:
Language specific rules:
e.g. how to treat “++”
Language-specific classifications:
keyword, symbol, identifier, integerCconstant, stringConstant,...
While we are at it, we can have the tokenizer record not only the token, but
also its lexical classification (as defined by the source language grammar).
7. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 7
Jack Tokenizer
if (x < 153) {let city = ”Paris”;}if (x < 153) {let city = ”Paris”;}
Source code
<tokens>
<keyword> if </keyword>
<symbol> ( </symbol>
<identifier> x </identifier>
<symbol> < </symbol>
<integerConstant> 153 </integerConstant>
<symbol> ) </symbol>
<symbol> { </symbol>
<keyword> let </keyword>
<identifier> city </identifier>
<symbol> = </symbol>
<stringConstant> Paris </stringConstant>
<symbol> ; </symbol>
<symbol> } </symbol>
</tokens>
<tokens>
<keyword> if </keyword>
<symbol> ( </symbol>
<identifier> x </identifier>
<symbol> < </symbol>
<integerConstant> 153 </integerConstant>
<symbol> ) </symbol>
<symbol> { </symbol>
<keyword> let </keyword>
<identifier> city </identifier>
<symbol> = </symbol>
<stringConstant> Paris </stringConstant>
<symbol> ; </symbol>
<symbol> } </symbol>
</tokens>
Tokenizer’s output
Tokenizer
8. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 8
Parsing
The tokenizer discussed thus far is part of a larger program called parser
Each language is characterized by a grammar.
The parser is implemented to recognize this grammar in given texts
The parsing process:
A text is given and tokenized
The parser determines weather or not the text can be generated from
the grammar
In the process, the parser performs a complete structural analysis of
the text
The text can be in an expression in a :
Natural language (English, …)
Programming language (Jack, …).
9. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 9
Parsing examples
(5+3)*2 – sqrt(9*4) she discussed sex with her doctor
-
5
sqrt
+
*
3
2
9 4
*
Jack English
discussed
she sex
with
her doctor
parse 1
discussed
she with
her doctor
parse 2
sex
10. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 10
More examples of challenging parsing
We gave the monkeys the bananas because they were hungry
We gave the monkeys the bananas because they were over-ripe
I never said she stole my money
I never said she stole my money
I never said she stole my money
I never said she stole my money
I never said she stole my money
I never said she stole my money
I never said she stole my money
I never said she stole my money
Time flies like an arrow
11. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 11
Simple (terminal) forms / complex (non-terminal) forms
Grammar = set of rules on how to construct complex forms from simpler forms
Highly recursive.
A typical grammar of a typical C-like language
while (expression) {
if (expression)
statement;
while (expression) {
statement;
if (expression)
statement;
}
while (expression) {
statement;
statement;
}
}
if (expression) {
statement;
while (expression)
statement;
statement;
}
if (expression)
if (expression)
statement;
}
while (expression) {
if (expression)
statement;
while (expression) {
statement;
if (expression)
statement;
}
while (expression) {
statement;
statement;
}
}
if (expression) {
statement;
while (expression)
statement;
statement;
}
if (expression)
if (expression)
statement;
}
Code sample
program: statement;
statement: whileStatement
| ifStatement
| // other statement possibilities ...
| '{' statementSequence '}'
whileStatement: 'while' '(' expression ')' statement
ifStatement: simpleIf
| ifElse
simpleIf: 'if' '(' expression ')' statement
ifElse: 'if' '(' expression ')' statement
'else' statement
statementSequence: '' // null, i.e. the empty sequence
| statement ';' statementSequence
expression: // definition of an expression comes here
// more definitions follow
program: statement;
statement: whileStatement
| ifStatement
| // other statement possibilities ...
| '{' statementSequence '}'
whileStatement: 'while' '(' expression ')' statement
ifStatement: simpleIf
| ifElse
simpleIf: 'if' '(' expression ')' statement
ifElse: 'if' '(' expression ')' statement
'else' statement
statementSequence: '' // null, i.e. the empty sequence
| statement ';' statementSequence
expression: // definition of an expression comes here
// more definitions follow
Grammar
13. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 13
Recursive descent parsing
Parser implementation: a set of parsing
methods, one for each rule:
parseStatement()
parseWhileStatement()
parseIfStatement()
parseStatementSequence()
parseExpression().
Highly recursive
LL(0) grammars: the first token
determines in which rule we are
In other grammars you have to
look ahead 1 or more tokens
Jack is almost LL(0).
while (expression) {
statement;
statement;
while (expression) {
while (expression)
statement;
statement;
}
}
while (expression) {
statement;
statement;
while (expression) {
while (expression)
statement;
statement;
}
}
code sample
14. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 14
A linguist view on parsing
Parsing:
One of the mental processes involved
in sentence comprehension, in which
the listener determines the syntactic
categories of the words, joins them
up in a tree, and identifies the
subject, object, and predicate, a
prerequisite to determining who did
what to whom from the information in
the sentence.
(Steven Pinker,
The Language Instinct)
15. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 15
The Jack grammar
’x’: x appears verbatim
x: x is a language construct
x?: x appears 0 or 1 times
x*: x appears 0 or more times
x|y: either x or y appears
(x,y): x appears, then y.
’x’: x appears verbatim
x: x is a language construct
x?: x appears 0 or 1 times
x*: x appears 0 or more times
x|y: either x or y appears
(x,y): x appears, then y.
16. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 16
The Jack grammar (cont.)
’x’: x appears verbatim
x: x is a language construct
x?: x appears 0 or 1 times
x*: x appears 0 or more times
x|y: either x or y appears
(x,y): x appears, then y.
’x’: x appears verbatim
x: x is a language construct
x?: x appears 0 or 1 times
x*: x appears 0 or more times
x|y: either x or y appears
(x,y): x appears, then y.
17. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 17
Jack syntax analyzer in action
Class Bar {
method Fraction foo(int y) {
var int temp; // a variable
let temp = (xxx+12)*-63;
...
...
Class Bar {
method Fraction foo(int y) {
var int temp; // a variable
let temp = (xxx+12)*-63;
...
...
Syntax analyzer
Using the language grammar,
a programmer can write
a syntax analyzer program (parser)
The syntax analyzer takes a source text
file and attempts to match it on the
language grammar
If successful, it can generate a parse tree
in some structured format, e.g. XML.
<varDec>
<keyword> var </keyword>
<keyword> int </keyword>
<identifier> temp </identifier>
<symbol> ; </symbol>
</varDec>
<statements>
<letStatement>
<keyword> let </keyword>
<identifier> temp </identifier>
<symbol> = </symbol>
<expression>
<term>
<symbol> ( </symbol>
<expression>
<term>
<identifier> xxx </identifier>
</term>
<symbol> + </symbol>
<term>
<int.Const.> 12 </int.Const.>
</term>
</expression>
...
<varDec>
<keyword> var </keyword>
<keyword> int </keyword>
<identifier> temp </identifier>
<symbol> ; </symbol>
</varDec>
<statements>
<letStatement>
<keyword> let </keyword>
<identifier> temp </identifier>
<symbol> = </symbol>
<expression>
<term>
<symbol> ( </symbol>
<expression>
<term>
<identifier> xxx </identifier>
</term>
<symbol> + </symbol>
<term>
<int.Const.> 12 </int.Const.>
</term>
</expression>
...
Syntax analyzer
The syntax analyzer’s algorithm shown in this slide:
If xxx is non-terminal, output:
<xxx>
Recursive code for the body of xxx
</xxx>
If xxx is terminal (keyword, symbol, constant, or identifier) ,
output:
<xxx>
xxx value
</xxx>
18. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 18
JackTokenizer: a tokenizer for the Jack language (proposed implementation)
19. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 19
JackTokenizer (cont.)
20. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 20
CompilationEngine: a recursive top-down parser for Jack
The CompilationEngine effects the actual compilation output.
It gets its input from a JackTokenizer and emits its parsed structure into an
output file/stream.
The output is generated by a series of compilexxx() routines, one for every
syntactic element xxx of the Jack grammar.
The contract between these routines is that each compilexxx() routine should
read the syntactic construct xxx from the input, advance() the tokenizer
exactly beyond xxx, and output the parsing of xxx.
Thus, compilexxx()may only be called if indeed xxx is the next syntactic
element of the input.
In the first version of the compiler, which we now build, this module emits a
structured printout of the code, wrapped in XML tags (defined in the specs of
project 10). In the final version of the compiler, this module generates
executable VM code (defined in the specs of project 11).
In both cases, the parsing logic and module API are exactly the same.
21. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 21
CompilationEngine (cont.)
22. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 22
CompilationEngine (cont.)
23. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 23
CompilationEngine (cont.)
24. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 24
Summary and next step
(Chapter 11)Jack
Program
Toke-
nizer
Parser
Code
Gene
-ration
Syntax Analyzer
Jack Compiler
VM
code
XML
code
(Chapter 10)
Syntax analysis: understanding syntax
Code generation: constructing semantics
The code generation challenge:
Extend the syntax analyzer into a full-blown compiler that, instead of
generating passive XML code, generates executable VM code
Two challenges: (a) handling data, and (b) handling commands.
25. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 10: Compiler I: Syntax Analysis slide 25
Perspective
The parse tree can be constructed on the fly
Syntax analyzers can be built using:
Lex tool for tokenizing
Yacc tool for parsing
Do everything from scratch (our approach ...)
The Jack language is intentionally simple:
Statement prefixes: let, do, ...
No operator priority
No error checking
Basic data types, etc.
Richer languages require more powerful compilers
The Jack compiler: designed to illustrate the key ideas that underlie modern
compilers, leaving advanced features to more advanced courses
Industrial-strength compilers:
Have good error diagnostics
Generate tight and efficient code
Support parallel (multi-core) processors.