Programming Language Concepts Flashcards
What is aliasing?
When 2 variables point to the same memory location
Static vs dynamic execution time
Static: before execution - compile time
Dynamic: during execution - runtime
What is the extent of a variable?
The time between allocation & deallocation
What is the scope of a variable?
The part of the code in which the variable can be referenced
Lexical vs dynamic scope
Lexical: scope determined by variable location
Dynamic: scope determined by sequence of function calls
Syntax vs semantics
Syntax: the structure of statments
Semantics: the meaning and behavior of syntactically correct statements and expressions
Concrete vs abstract syntax tree
Concrete: includes all elements explicitly as they appear in the source (a lot of unnecessary nodes)
Abstract: omits unnecessary syntactic details for simplicity & clarity
How can a grammar be ambiguous?
If a string has more than one valid derivation (different parse trees)
How can ambiguity be removed from a grammar?
Use precedence: higher precedence operators (*) appear lower in the tree (than +)
Use left associativity: 2+3+4 = (2+3)+4 (left) and put higher precedence operators on the left (<expr> ::= <expr> + <mexpr>
)
What is a lexeme?
A pattern of characters in the input string that are considered meaningful e.g. “while” is a lexeme identified as the while command token
How are lexemes identified?
With a scanner
e.g. for input “…123,456…” & pattern [1-9]+, has lexemes “123” & “456”. if pattern was [1-9]+,[1-9]+, lexemes “123,456”
What is maximal munch?
Where the scanner consumes the longest possible sequence of characters that form a valid token
What are lexer generators?
A software tool that:
1) Takes input
2) Describes lexemes and what tokens they produce
3) Generates code for you that performs lexical analysis
Top-down vs bottom-up approach to parsing
Top-down: builds parse tree from root down (uses LL parsers)
Bottom-up: builds parse tree from leaves up (uses LR parsers)
What are LL and LR parsers?
LL: reads parse trees from left to right and forms leftmost derivations
LR: reads parse trees from left to right and forms rightmost derivations
Left recursive vs right recursive grammars
LRG: A -> Aa | B
RRG: A -> aA | B
What is shift-reduce?
A bottom-up parsing technique where the parser shifts input symbols onto a stack until a rule can be applied to reduce the stack’s top elements to a non-terminal, gradually building up a parse tree from the input
What is a shift-reduce conflict?
When the parser can either shift an expression (1+2+3 -> 1+2+3) or reduce it (1+2+3 -> 3+3)
What 2 ways can shift-reduce conflicts be avoided?
Layering non-terminals: defining separate non-terminal symbols for different levels of operator precedence (higher-level terminals defined in terms of lower-level terminals)
Specify precedence: use directives such as %left (%left ‘+’ ‘-‘ so “1+2-3” -> (1+2)+3) -> , %right, and %nonassoc (“1>2>3” is disallowed)
What is a parser generator?
A software that, given input describing rules of grammar and what to perform when each rule is used, will generate code for you that performs parsing
Static typing vs dynamic typing
Static: performs type checking at compile time; run time type values are approximated
Dynamic: performs type checking at run time; exact types used
Strong vs weak typing
Strong: when operation invoked, arguments provided are of the correct type
Weak: wrong types may be passed to a function and function chooses how to behave (e.g. coerces data to be required type)
What is type checking?
A part of compilation from statically typed languages
Done on AST of program
What is inversion lemma?
Where we infer types of subprograms from the type of the whole program by reading type rules from bottom to top