Midterm Exam
How would you remove left-recursion from the following grammar? <A> -> <A>a<B> | <A>bb<A> | <B>a | <C><B><A> <B> -> b<C> | <C> <C> -> c
<A> -> <B>a<A'> | <C><B><A><A'> <A'> -> a<B><A'> | bb<A><A'> | ε <B> -> b<C> | <C> <C> -> c
How would you remove the direct left-recursion from the following grammar? <A> -> <A>a<B> | <B>a | <C><B><A> <B> -> b<C> | <C> <C> -> c
<A> -> <B>a<A'> | <C><B><A><A'> <A'> -> a<B><A'> | ε <B> -> b<C> | <C> <C> -> c
How would you resolve the pairwise disjointness problem in the grammar below? <X> -> x<Y> | x<Z> | xa<X> | <Y>x | <Z>x<Y> <Y> -> y<Z> | <Z> <Z> -> z
<X> -> x <new> | <Y>x | <Z>x<Y> <new> -> <Y> | <Z> | a<X> <Y> -> y<Z> | <Z> <Z> -> z
Can you resolve the pairwise disjointness of the following rule? Do so by creating a new rule (not by using EBNF notation). A --> a | aB
A --> aA' A' --> ε | B
What is the purpose of a lexer? a. A lexer scans tokens to determine whether the tokens appear in an order that satisfy the grammar rules of a given language b. A lexer produces a parse tree c. A lexer scans an input file and identifies or associates tokens with groups of characters that match a particular token category. d. A lexer is responsible for executing a left-most derivation
A lexer scans an input file and identifies or associates token with groups of characters that match a particular token category
The key difference between a left-most and right-most derivation is that: a. left-most derivations always start with the starting rule (or at the root of the tree) while right-most derivations always start with the input string and work their way up to the starting rule (or start at the leaves and work towards the root) b. Given a sentential form that contains multiple non-terminals, a left-most derivation always picks the left-most non-terminal to reduce next, while a right-most derivation always picks the right-most non-terminal to reduce next c. A right-most derivation uses the handle-pruning technique to derive a sentence, while a left-most derivation uses the left-recursive technique to derive a sentence. d. Left-most derivations are generators and right-most derivations are recognizers.
Given a sentential form that contains multiple non-terminals, a left-most derivation always picks the left-most non-terminal to reduce next, while a right-most derivation always picks the right-most non-terminal to reduce next
(def on opposite side) add id to the stack, add 1 to the stack, advance the input string so that the . is after the first id symbol, transition to state 1, reduce by rule 3, and pop one pair of symbols off the stack
Given the following parser table, state of the stack, and input string, if you were to read in the next symbol, what action would you take? Tip: Recall that when representing how much of an input string we've read, everything BEFORE the .represents what we have already read in, and everything AFTER the . represents what we haven't seen yet.
A grammar is considered ambiguous if: a. Some of the rules in the grammar have more than one definition. b. It is possible to generate a sentential form that has more than one distinct parse tree. c. You cannot produce a parse tree from it. d. It is left-recursive or if it contains pairwise disjointness.
It is possible to generate a sentential form that has more than on distinct parse tree
record types
VS arrays, record elements are referenced by an identifier, rather than an index
What is a grammar?
a finite non-empty set of rules
what is a union
a type whose variables are allowed to store different type values at different times during execution
what is the FIRST set for the A rule in the grammar below? A -> aB | bAb | Bb B -> cB | d
a, b, c, d
What is BNF (Backus-Naur Form)?
abstractions used to represent classes of syntactic structures terminals(lexemes or tokens), nonterminal symbols
ACTION and GOTO tables
action - specifies the action of the parser, given the parser states and the next token goto - specifies which state to put on top of the parse stack after a reduction action is done
what is it called when two variables names can be used to access the same memory location
aliases (harmful to readability)
explicit heap-dynamic
allocated and deallocated by explicit directives, specified by the programmer, which take effect during execution
implicit heap-dynamic
allocation and deallocation caused by assignment statements
What does it mean for a grammar to be ambiguous
ambiguous if and only if it generates a sentential form hat has two or more distinct parse trees
What is a variable?
an abstraction of a memory cell Ex. Name, address, value, type, lifetime, scope
array operations
an array operation is one that operates on an array as a unit
two fundamental operations
assignment and dereferencing assignment - used to seta. pointer variable's value to some useful address dereferencing yields the value stored at the location represented by the pointers value
what is binding?
association between an entity and an attribute, such as between a variable and its type or value, or between an operation and a symbol (binding time- time at which the binding takes place)
heap-dynamic
binding of subscript ranges and storage allocation is dynamic and can change any number of times
what are Java's signed integer sizes
byte, short, int, long
(def on opposite side) token --> lexeme var --> A,b,c,d Operations --> +,-,= const --> const semi --> ;
can you identify what tokens this grammar might consist of and match them with possible lexemes?
locating an element in a multi-dimensioned array
ch 6 pg 39
what is the referencing environment
collection of all names that are visible in the statemtn
heap managment
complex runtime process two approaches to reclaim garbage- reference counters, mark-sweep
What is the "value" of a variable?
contents of the location with which the variable is associated l-value is its address r-value is its value
when are stack-dynamic-storage bindings created?
created for variable when their declaration statements are elaborated (a statement is elaborated when the executable code associated with it is executed) if scalar, all attributes except address are statically bound
problems with pointers
dangling pointers- a pointer points to a heap-dynamic variable that has been deallocated lost heap-dynamic variable - An allocated heap-dynamic variable that is no longer accessible to the user program(garbage)
what is implicit declaration?
default mechanism for specifying types of variables through default conventions, rather than declaration statements
lists in lisp and scheme
delimited by parentheses and use no commas
What is meant by "type" of a variable?
determines the range of values of variables and the et of operations that are defined for values of that type; in the case of floating point, type also determines the precision
How do nonterminals often appear?
enclosed in angle brackets <ident_list> --> identifier
static scope
enclosing static scopes are called its static ancestors; the nearest static ancestor is called a static parent
true or false: a variable cannot have different addresses at different times during execution
false
true or false: if we use the parse tree to indicate precedence levels of the operators, we can have ambiguity
false
true or false: scope and lifetime are the same concept
false
What is allocation?
getting a cell from some pool of available cells
jagged matrix
has rows with varying number of elements, possible when multi-dimensioned arrays actually appear as arrays of arrays
what is a syntax analyzer(parser)
high level part; mathematically, a push-down automaton based on a context-free grammar, BNF)
(def on opposite side) +, -, =, ;, a, b, c, d, const
identify all lexemes
left recursion problem
if a grammar has left recursion, either direct or indirect, it cannot be the basis for a top-down parser review examples in ch4 pg 34
What does static binding mean
if it first occurs before run time and remains unchanged throughout program execution
what does dynamic binding mean?
if it first occurs during execution or can change during execution of the program and can change via an assignment statement
when is a subprogram active?
if its execution has begun but has not yet terminated
what are the primitive data types?
integer, floating point, decimal, boolean, character
A token: a. is a category or name for a group of characters that comprise the smallest unit of a programming language b. is a group of characters that comprise the smallest unit of a programming languages. c. is a type of context free grammar. d. is a recognizer for a grammar
is a category or name for a group of characters that comprise the smallest unit of a programming language
heterogeneous array
is one in which the elements need not be of the same type
What is a data type
it defines a collection of data objects and set of predefined operations outhouse objects
jagged vs rectangular arrays
key distinction: jagged arrays are array that point to other arrays, while rectangular arrays are represented as a single matrix
What are the possible binding times?
language design time - bind operator symbols to operations (*, - ,+) language implementation time - bind floating point type to a representation compile time - bind a variable to a type in C or Java load time - bind a C or C++ static variable to a memory cell runtime - bind a non static local variable to a memory cell
Lexeme vs. Token
lexeme: lowest level syntactic unit of a language token: a category of lexemes (identifier)
local, global, and nonlocal variables
local - those that are declared in that unit global - special category of nonlocal variables nonlocal- those that are visible in the unit but not declared there
what is a lexical analyzer?
low-level part: mathematically, a finite automaton based on a regular grammar
indexing
mapping from indices to elements
what is the address of a variable?
memory address with which it is associated
rectangular array
multi-dimensioned array in which all of the rows have the same number of elements and all columns have the same number of elements
Do variables have to all have names?
no
What is a leftmost derivation
one in which the leftmost nonterminal in each sentential form is the one that is expanded
pointers in C and C++
pointers can point at any variable regardless of when or where it was allocated used for dynamic storage management and addressing pointer arithmetic is possible
what is a record?
possibly heterogeneous aggregate of data elements in which the individual elements are identified by names
bottom up parser
produce the parse tree, beginning at the leaves order is that of the reverse of a right most derivation
Top down parser
produce the parse treen, beginning at the root order is that of a leftmost derivation traces or builds the parse tree in preorder
A top down parser: a. Produces a parse tree that is in the order of a left-most derivation. It examines the input from right-to-left b. Produces a parse tree that is in the order of a left-most derivation. It examines the input from left-to-right. c. Produces a parse tree that is in the order of a right-most derivation. It examines the input from left-to-right d. Produces a parse tree that is in the order of a right-most derivation. It examines the input from right-to-left.
produces a parse tree that is in the order of a left-most derivation. it examines the input from left-to-right
What is explicit declaration?
program statement used for declaring the types of variables
what is deallocation
putting a cell back into the pool
how do we let an interpreter know what data is a list?
quote with an apostrophe ex '(A B C)
what is scope?
range of statements over which it is visible
recognizer vs. generators
recognizer: device that reads input strings over the alphabet of the language and decides whether the input strings belong to the language generator: device that generates sentences of a language
What is reducing and shifting?
reduce:action of replacing the handle eon the top of the parse stack with its corresponding LHS shift: moving the next token to the top of the parse stack
What is a derivation?
repeated application of rules, starting with the start symbol and ending with a sentence
what is an object?
represents an instance of a user-defined (abstract data) type
what is the search process?
search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name
semantics vs. syntax
semantics: the meaning of the expressions, statements, and program units syntax: the form or structure of the expressions, statements, and program units
What are sentences and languages?
sentences: a string of characters over some alphabet language: a set of sentences
fixed heap-dynamic
similar to fixed stack-dynamic: storage binding is dynamic but fixed after allocation
What is an example of dynamic type binding?
specified through an assignment statement list = [2, 4.33, 6, 8]; list = 17.3;
*******what is the lifetime of a static variable?*********NEED TO KNOW
static -- bound to memory cells before execution begins and remains bound to the same memory cell throughout execution
static
subscript ranges are statically bound and storage allocation is static
fixed stack-dynamic
subscript ranges are statically bound, but the allocation is done at declaration time
what is a slice?
substructure of an array; nothing more than a referencing mechanism, only useful in languages that have array operations
what is a descriptor
the collection of the attributes of a variable
What is the handle?
the handle or a right sentential form is its leftmost simple phrase(examples on ch 4 pg 53)
what is an abstract memory cell?
the physical cell or collection of cells associated with a variable
what is the lifetime of a variable
the time during which it is bound to a particular memory cell
primitive data types
those not defined in terms of other data types
dangling pointer problem
tombstone - extra heap cell that is a pointer to the heap-dynamic variable locks-and-keys: pointer values are represented as (key, address) pairs
True or false: an abstraction can have more than one RHS
true
True or false: variables can be hidden from a unit by having a "closer" variable with the same name
true
true or false: EBNF minimizes the number of nonterminals
true
true or false: a variable may have different addresses at different places in a program
true
true or false: an array is a homogeneous aggregate of data elements in which an individual element is identified by its positions in the aggregate relative to the first element
true
pointer
type variable has a range of values that consists of memory addresses and a special value, nil provide the power of indirect addressing provide a way to manage dynamic memory a pointer can be used to access a location in the area where storage is dynamically created
what is an associative array?
unordered collection of data elements that are indexed by an equal number of values called keys
character string types:
values are sequences of characters
does perl allow negative indexes
yes