0% found this document useful (0 votes)
89 views7 pages

Overview of Abstract Stack Machine

The document discusses abstract stack machines, the Red compiler, and FLEX. It then asks two questions about parse trees and syntax trees. 1. It provides an overview of abstract stack machines and their basic components: the workspace, stack, and heap. It also briefly describes the Red compiler and FLEX tool. 2. It lists two differences between parse trees and syntax trees: parse trees follow precedence and operator depth while syntax trees ignore precedence and condense productions. It asks to construct examples of each.

Uploaded by

kefiyalew kunta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views7 pages

Overview of Abstract Stack Machine

The document discusses abstract stack machines, the Red compiler, and FLEX. It then asks two questions about parse trees and syntax trees. 1. It provides an overview of abstract stack machines and their basic components: the workspace, stack, and heap. It also briefly describes the Red compiler and FLEX tool. 2. It lists two differences between parse trees and syntax trees: parse trees follow precedence and operator depth while syntax trees ignore precedence and condense productions. It asks to construct examples of each.

Uploaded by

kefiyalew kunta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

[Link] bindings are added to stack when let expression is simplified.


when an identifier is encountered during simplification, assoc value can be found
in [Link] 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 [Link] 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.

[Link] 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

Common questions

Powered by AI

The ASM refines the notion of value and computation model by explicitly representing and managing data locations within computer memory. This is crucial in the presence of mutable state as it provides a structured approach to model computations and substitutions using a stack. It tracks where data structures reside in memory and simplifies expressions or commands through its workspace, stack, and heap components, thereby addressing mutable states effectively by providing a transparent, logical flow of data and state throughout execution .

Flex works in conjunction with GNU Bison to create robust parsers and scanners. Flex generates lexical analyzers, or scanners, while GNU Bison generates parsers. When provided with a .l file, Flex automatically generates the yylex() function, which retrieves tokens for Bison's parser from the current token stream. This function is integral for the parser as it processes inputs during syntax analysis. Together, they provide a flexible and efficient way to parse and analyze language syntax, with Flex handling the preliminary tokenization and Bison performing the syntactic evaluation .

The Abstract Stack Machine (ASM) manages program execution through three main components: the workspace, stack, and heap. The workspace keeps track of the expression or command currently being simplified, changing its contents as the program evaluates. The stack maintains a sequence of bindings that map identifiers to their values, adding new bindings when let expressions are simplified and locating associated values when identifiers are encountered. It also keeps track of partially simplified expressions. Lastly, the heap models the computer's memory, storing non-primitive data values. Together, these components enable ASM to refine value and computation models, tracking data structures' memory locations effectively .

The Red compiler allows for easy development of applications with GUI components through its simple and accessible built-in GUI system. This includes an intuitive structure for creating interfaces, as demonstrated with basic commands to create text fields or buttons that execute print commands, making GUI programming straightforward. Additionally, the Red binary includes a complete toolchain for native compilation, facilitating standalone applications with no additional dependencies, enhancing end-user convenience and deployment .

Parse trees and syntax trees differ mainly in their representation of syntax. Parse trees are detailed representations of the derivation from grammar to input string, following operator precedence, with root nodes representing the start symbol and adhering to context-free grammar rules. Syntax trees, on the other hand, condense parse trees by moving operators and keyword nodes to their parents and simplifying individual productions to single links, focusing more on syntactic structures. These differences impact compiler design by making syntax trees more efficient for semantic analysis and syntax-directed translation, reducing unnecessary detail while retaining essential structure .

The Abstract Stack Machine (ASM) simplifies the understanding of mutable state in programming by abstracting data memory locations and offering a structured model to track state changes explicitly. It does this by using a combination of workspace, stack, and heap to manage variable bindings, ongoing computations, and non-primitive data storage with clarity. This design allows programmers to follow state transitions and manipulations directly, reducing complexity in tracking how state changes over time during program execution, thereby aiding in the conceptual grasp of mutable states .

In the Red compiler, creating a 'Hello World' application using the interpreter involves running the Red binary with no arguments to access the REPL, then executing 'print "Hello World!"' directly for immediate output. In contrast, the compiled binary approach involves writing the program and compiling it into a standalone binary file using Red's toolchain. This compiled version does not require a separate runtime or dependencies, providing a prepackaged, distributable application. The interpreter approach is suitable for development and testing, whereas the compiled binary is ideal for deployment and execution without Red's runtime .

FLEX improves performance and flexibility over its predecessors, Lex and Yacc, by generating faster code and offering more flexibility in lexical analysis processes. Flex is integrated with GNU Bison, enabling a more efficient parsing mechanism by generating tailored scanners. This is beneficial for handling complex language syntax with optimized processing speed and adaptability to varied programming structures, thereby enhancing versatility and efficiency in compiler construction and maintenance .

To install Flex on an Ubuntu system, you must first update the system using 'sudo apt-get update' to ensure all packages are up to date. Following the update, Flex can be installed using 'sudo apt-get install flex.' It's important to update the system first because an outdated package may not work with other installed packages, or it might no longer be available, potentially causing compatibility issues or failures during installation .

The yylex() function, generated by Flex, is critical in the cooperation between Flex and GNU Bison as it serves as the lexical analyzer's core function, tasked with retrieving tokens from the current token stream for the parser. This function runs the Rule Section, converting input into meaningful tokens that Bison's parser processes for syntax analysis. Its role is indispensable because it forms the bridge between lexical and syntactic analysis, providing the essential data flow required for parsing complex language constructs efficiently .

You might also like