You are on page 1of 7

COLLEGE OF COMPUTING AND INFORMATICS

Department:computer science
STUDENT NAME:kefiyalew kunta
STUDENT ID:0841
COURSE CODE:Cosc3102
COURSE TITLE:Compiler Design

Submitted to| Mr Kolana K]


1. Explain Abstract Stack Machine, Red Compiler and FLEX briefly.

The Abstract Stack Machine

 abstract stack machine: model programs in presence of mutable state.


 ASM properly accounts for locations of data in the computer's memory

hides many of the details of the computer's actual memory structure &

representation of data.

Parts of the ASM

values: finished results such as integers, tuples of values, records, functions,

or constructors applied to values.

ex. 0, 1, (1, (2, 3)), {x = 0; y = 1}, Cons(3, Nil)

expressions: computations in progress

ex. 1 + 2 * 3, (f x), p.x, begin match...with...end

ASM gives explicit algorithm for implementing substitution using a stack,

refines notion of value & computation model to keep track of where in memory

data structures reside.

3 basic parts of ASM model

workspace: keeps track of expression or command that computer is

currently simplifying, as program evaluates, contents of workspace change

stack: keeps track of a sequence of bindings that map identifiers to their

values.new bindings are added to stack when let expression is simplified.


when an identifier is encountered during simplification, assoc value can be found
in stack.keeps track of partially simplified expressions

heap: models computer's memory, used for storage of non-primitive data values

Red compiler

Red comes with an interpreter in addition to the compiler,which can be easily


accessed using the built-in REPL.Calling Red binary wiith no argument will open
the console and allow you to interact with the language in live:

red>> print “Hello World!”

Hello world!

If you are running Red from windows,you can also use the built in GUI system and
make a more appealing Helloworld:

red>> view[text “Hello World!”]

Now try something more sophisticated:

red>> view [name: field button "Hi" [print ["Hi" name/text]]]

Yes, GUI programming can be that easy! See more about GUI capabilities in this
GUI release post and have a look into the View reference documentation.

Compiling a "Hello World"

You can also compile your Red programs and get a single binary with no
dependencies. You don't have to install anything else, the Red binary you have
downloaded already contains a complete toolchain for native compilation!

FLEX (fast lexical analyzer generator) is a tool/computer program for


generating lexical analyzers (scanners or lexers) written by Vern Paxson in C
around 1987. It is used together with Berkeley Yacc parser generator or GNU
Bison parser generator. Flex and Bison both are more flexible than Lex and Yacc
and produces faster code.
Bison produces parser from the input file provided by the user. The function
yylex():is automatically generated by the flex when it is provided with a .l file and
this yylex() function is expected by parser to call to retrieve tokens from
current/this token stream.

Note: The function yylex() is the main flex function that runs the Rule Section and
extension (.l) is the extension used to save the programs.

Installing Flex on Ubuntu:

sudo apt-get update


sudo apt-get install flex

Note: If Update command is not run on the machine for a while, it’s better to run it
first so that a newer version is installed as an older version might not work with the
other packages installed or may not be present now.

2.Write at least two differences b/n Parse tree and Syntax tree &
illustrate them with example

Parse Tree

Parse tree is a hierarchical structure that defines the derivation of the grammar to
yield input strings. In parsing, the string is derived using the start symbol. The root
of the parse tree is that start symbol. It is the graphical description of symbols that
can be terminals or non-terminals. Parse tree follows the precedence of operators.
The deepest sub-tree traversed first. Therefore, the operator in the parent node has
less precedence over the operator in the sub-tree.

A Parse Tree for a CFG G = (V, Σ, P, S) is a tree satisfying the following


conditions −

 Root has the label S, where S is the start symbol.


 Each vertex of the parse tree has a label which can be a variable (V),
terminal (Σ) or ε.
 If A → C1, C2 … … . Cn is a production, then C1, C2 … … . Cn are children
of node labeled A.
 Leaf Nodes are terminal (Σ), and Interior nodes are variable (V).
 The label of an internal vertex is always a variable.
 If a vertex A has k children with labels A1, A2 … … . Ak, then A → A1, A2
… … . Ak will be production in context-free grammar G.

Syntax Tree

A syntax tree is a tree that displays the syntactic structure of a program while
ignoring inappropriate analysis present in a parse tree. Thus, the syntax tree is
nothing more than a condensed form of the parse tree. The operator and keyword
nodes of a parse tree are shifted to their parent and a group of individual
production is replaced by an individual link. For example, for a given parse tree of
string id + id * id.

Syntax Tree for the expression is as follows −

Example− Construct

 Parse Tree
 Syntax Tree
 Annotated for complete parse tree for the input string 1 * 2 + 3 by using any
grammar you know.

Solution

You might also like