






























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!































Digital computers can do anything that can conceivably be done with information: they are general purpose information processing machines
You get a computer to do something by programming it to run algorithms that manipulate data structures
Picking what algorithms and data structures are suitable for a particular application is something every good programmer needs to know
One important application area is: writing programs that understand programs…
An interpreter reads a line or statement at a time of a source language text, and then does what that line or statement says to do
The source language should have clearly defined syntax and semantics rules the syntax rules say what strings legally belong to the source language, and what their structure is the semantics rules say what each legal string in the language means, based on its structure
Without clearly defined syntax and semantics, writing an interpreter isn’t really possible!
A language interpreter does two things with each line or statement in the input: syntactic analysis and semantic evaluation
Syntactic analysis is done in accordance with the syntax rules for the language
Semantic evaluation is done in accordance with the semantic rules
The result of all this is: doing what the statement says! (For example, computing a value and returning it or printing it out)
The syntax rules for the language can be written down in Backus-Naur Form (BNF) or a similar notation
A BNF grammar is a list of syntax rules
Each rule defines one "nonterminal symbol", which appears at the left of a " := " sign in the rule
Alternative definitions of the nonterminal symbol appear to the right of the " := " sign, separated by " | " signs
Often the definition of a nonterminal in a BNF grammar is recursive: it defines the nonterminal in terms of itself
The nonterminal symbol defined in rule listed first in the grammar is called the "start" symbol of the grammar
A symbol not defined by a rule in the grammar is a "terminal symbol", and is usually taken literally
If a string satisfies the definition of the "start" symbol, it is in the language defined by the BNF grammar; otherwise not
The process of using the grammar to check to see if a string is in the language is called parsing the string
Here is a BNF grammar for a simple formal language
The "start” symbol for this grammar is
If a string satisfies the definition of , it is in the language
defined by this grammar; otherwise not
:= = := + | - | := * | / | := | | () := w | x | y | z := 0 | 1 | 2 | 3 | 4
=> => => => => 2
=> => + => + =>
For every derivation, there is a corresponding tree: a parse tree
Each node in the parse tree corresponds to one symbol in the BNF grammar
Leaves in the parse tree correspond to terminal symbols; internal nodes correspond to nonterminal symbols
The root of the parse tree is the "start" symbol
The children of an internal node in the parse tree correspond to the symbols in a definition of the nonterminal symbol corresponding to their parent node
Reading the leaves of the parse tree left to right gives you the string that has been parsed
How to build a parse tree?
Given a derivation of a string, you could build a parse tree “top down”: The start symbol is the root of the tree. Children of the root are symbols in the definition of the start symbol used to create the next step in the derivation. In general, children of a node in the parse tree are symbols in the definition of a nonterminal that was used to create a next step in the derivation.
2
3
(^) +
2
w
A parse tree includes all the information in a derivation of a string from the start symbol In a parse tree, the children of a nonterminal symbol node include nodes for all of the symbols in the definition of that nonterminal
But this may be more information than is required for some applications of the tree
For example: do not need to include nodes for parentheses just used for grouping; information in nodes corresponding to expression operators can be moved into their parent nodes; etc.
A parse tree with nonessential nodes left out is called an Abstract Syntax Tree (AST)
1
( (^) )
2
3
1
2
3