Cheat Sheet Updated
Cheat Sheet Updated
lis2))
<LHS> → <RHS> all of the work for you. ((NULL? lis2) #F)
LHS:abstraction being defined -Lex also provides a count yyleng of the number of ((equal (CAR lis1) (CAR lis2))
RHS:definition characters matched. (equal (CDR lis1) (CDR lis2)))
“→” means "can have the form" -yywrap is called whenever lex reaches an end-of-file (ELSE #F)))
::= is used for → -Lex is a tool for writing lexical analyzers. 15.(DEFINE (append lis1 lis2)
<assign> → <var> = <expression>,this is a rule.var and -Yacc is a tool for constructing parsers. (COND
expression be defined. Yacc ((NULL? lis1) lis2)
-These abstractions are called variables or nonterminals -Yacc stands for yet another compiler to compiler. Reads (ELSE (CONS (CAR lis1)
-lexemes and tokens are the terminals a specification file that codifies the grammar of a (append (CDR lis1) lis2)))))
<ident_list> → identifier | identifier, <ident_list> language and generates a parsing routine. *(append '(A B) '(C D R)) returns (A B C D R)
<if_stmt> → if <logic_expr> then <stmt> -Yacc specification describes a Context Free Grammar *(append '((A B) C) '(D (E F))) returns ((A B) C D (E F))
A rule has a left-hand side (LHS), which is a nonterminal, (CFG), that can be used to generate a parser. 16.(DEFINE (quadratic_roots a b c)
and a right-hand side (RHS), which is a string of terminals Elements of a CFG: (LET (
and/or nonterminals. 1. Terminals: tokens and literal characters, (root_part_over_2a
<LHS> → <RHS> 2. Variables (nonterminals): syntactical elements, (/ (SQRT (- (* b b) (* 4 a c)))(* 2 a)))
Grammar: a finite non-empty set of rules. 3. Production rules, and (minus_b_over_2a (/ (- 0 b) (* 2 a)))
Example: 4. Start rule. (DISPLAY (+ minus_b_over_2a root_part_over_2a))
Mary greets Alfred -Format of a yacc specification file: (NEWLINE)
<sentence> ::= <subject><predicate> declarations (DISPLAY (- minus_b_over_2a root_part_over_2a))))
<subject> ::= <noun> %% 17.Original: (DEFINE (factorial n)
<predicate> ::= <verb><object> grammar rules and associated actions (IF (= n 0)
<verb> ::= greets %% 1
<object> ::= <noun> C programs (* n (factorial (- n 1))) ))
<noun> ::= Mary | John | Alfred -Declarations Tail Recursive: (DEFINE (facthelper n factpartial)
Grammars and Derivations %token: declare names of tokens (IF (= n 0)
-The sentences of the language are generated through a %left: define left-associative operators factpartial
sequence of applications of the rules, starting from the %right: define right-associative operators facthelper((- n 1) (* n factpartial)))))
special nonterminal called start symbol.Such a generation %nonassoc: define operators that may not associate with (DEFINE (factorial n)
is called a derivation. themselves (facthelper n 1))
If always the leftmost nonterminal is replaced, then it is %type: declare the type of variables 18.(DEFINE (compose f g) (LAMBDA (x) (f (g x))))
called leftmost derivation. %union: declare multiple data types for semantic values ((compose CAR CDR) '((a b) c d)) yields c
A grammar is ambiguous if and only if it generates a %start: declare the start symbol (default is the first 19.(DEFINE (third a_list)
sentential form that has two or more distinct parse trees. variable in rules) ((compose CAR (compose CDR CDR)) a_list)) is
<assign> ::= <id> = <expr> %prec: assign precedence to a rule equivalent to CADDR
<id> ::= A | B | C %{ C declarations directly copied to the resulting C 20.(DEFINE (map fun lis)
<expr> ::= <expr> + <expr> program %} (COND
| <expr> * <expr> $$: left-hand side ((NULL? lis) ())
| (<expr>) $1: first item in the right-hand side (ELSE (CONS (fun (CAR lis))
| <id> $n: nth item in the right-hand side (map fun (CDR lis))))))
Parse trees of A = B + C * A -Yacc provides a special symbol for handling errors. The 21.(map (LAMBDA (num) (* num num num)) '(3 4 2 6))
symbol is called error and it should appear within a yields (27 64 8 216)
grammar-rule. -yylex() function returns an integer, the 22.((DEFINE (adder lis)
token number, representing the kind of token read. If (COND
there is a value associated with that token, it should be ((NULL? lis) 0)
assigned to the external variable yylval. (ELSE (EVAL (CONS '+ lis)))))
23.(DEFINE sum
(lambda (l)
(if (null? l)
0
(+ (car l) (sum (cdr l))))))
24.(DEFINE product
(lambda (l)
<expr> -> <expr> + <expr> | const (ambiguous)
(if (null? l)
<expr> -> <expr> + const | const (unambiguous)
1
-In a BNF rule, if the LHS appears at the beginning of the
(* (car l) (product (cdr l))))))
RHS, the rule is said to be left recursive. Left recursion
25.(DEFINE length
specifies left associativity.
(lambda (l)
<expr> ::= <expr> + <term>
(if (null? l)
| <term>
0
-most of the languages exponential is defined as a right
(+ 1 (length (cdr l))))))
associative operation
26.(DEFINE reverse
<factor> ::= <expr> ** <factor>
(lambda (l)
| <expr>
Scheme (if (null? l)
<expr> ::= (<expr>)
1.(DEFINE (compare x y) nil
| <id>
(COND (append (reverse (cdr l)) (list (car l))))))
Extended BNF: Optional parts are placed in brackets [ ].
((> x y) “x is greater than y”) 27.(DEFINE (third list)
Repetitions (0 or more) are placed inside braces { }.
((< x y) “y is greater than x”) (caddr list))
Alternative parts of RHSs are placed inside parentheses
(ELSE “x and y are equal”) ) ) (third '(1 2 3 4 5 6 7)) returns 3
and separated via vertical bars.
2.(CONS 'A '(B C)) returns (A B C) 28. (DEFINE a 1)
Designing Patterns(Lex)
3.(LIST ′apple ′orange ′grape) return (apple orange grape) (DEFINE b 2)
[abc] matches a, b or c
4.(CAR '(A B C)) yields A (DEFINE c 3)
[a-f] matches a, b, c, d, e, or f
5.(CAR '((A B) C D)) yields (A B) (let ((a 2)
[0-9] matches any digit
6.(CDR '(A B C)) yields (B C) (b (+ a 7))
X+ matches one or more of X
7.(CDR '((A B) C D)) yields (C D) (c b))
X* matches zero or more of X
8.(LIST? '()) yields #T (+ a b c)) returns 12
[0-9]+ matches any integer
9.(NULL? '(())) yields #F 29.(DEFINE (my-func f)
(…) grouping an expression into a single unit
10.(DEFINE (member atm lis) (lambda (x y) (f (f x y) (f x y))))
(a|b|c)* is equivalent to [a-c]*
(COND (my-func *) 2 4) returns 64
X? X is optional (0 or 1 occurrence)
((NULL? lis) #F) 30.(DEFINE (smallest x y z)
if(def)? matches if or ifdef (equivalent to if|ifdef)
((EQ? atm (CAR lis)) #T) (min x y z))
[A-Za-z] matches any alphabetical character
((ELSE (member atm (CDR lis))))) Subprograms
. matches any character except newline character
11.(DEFINE (second a_list) (CAR (CDR a_list))) 1.Pass-by-value(In Mode): Changes made to the function do
\. matches the . character
(second '(A B C)) = returns B not change the actual parameter.
\n matches the newline character
12.(CADDAR x) = (CAR (CDR (CDR (CAR x)))) 2.Pass-by-result(Out Mode): No value is transmitted to
\t matches the tab character
(CADDAR '((A B (C) D) E)) = returns (C) the subprogram.The corresponding formal parameter
\\ matches the \ character
13.(DEFINE (equalsimp lis1 lis2) acts as a local variable.Its value is transmitted to caller’s
[ \t] matches either a space or tab character
(COND actual parameter when control is returned to the
[^a-d] matches any character other than a,b,c and d
((NULL? lis1) (NULL? lis2)) caller.Require extra storage location and copy operation.
Real numbers
((NULL? lis2) #F) Example: Subprogram sub(x, y) { x <- 3 ; y <-5;}
[0-9]*(\.)?[0-9]+
((EQ? (CAR lis1) (CAR lis2)) call:
To include an optional preceding sign:
(equalsimp(CDR lis1)(CDR lis2))) sub(p, p)
[+-]?[0-9]*(\.)?[0-9]+
(ELSE #F))) what is the value of p here ? (3 or 5?)
Integer or floating point number
14.(DEFINE (equal lis1 lis2) -The values of x and y will be copied back to p. Which
[0-9]+(\.[0-9]+)?
(COND ever is assigned last will determine the value of p.
Integer, floating point or scientific notation.
((NOT (LIST? lis1))(EQ? lis1 lis2)) -The order is important.
[+-]?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?
((NOT (LIST? lis2)) #F) 3.Pass-by-value-result(Inout Mode): A combination of
pass-by-value and pass-by-result.Formal parameters have Implementing Dynamic Scoping
local storage.The value of the actual parameter is used to -Deep Access: non-local references are found by searching
initialize the corresponding formal parameter. the activation record instances on the dynamic chain.
-The formal parameter acts as a local parameter. Length of the chain cannot be statically determined.
-At termination, the value of the formal parameter is Ength of the chain cannot be statically determined.
copied back. -Shallow Access: put locals in a central place.
4.Pass-by-reference(Inout Mode): Changes made to the One stack for each variable name.
function do change the actual parameter. Central table with an entry for each variable name.
5.Pass-by-name(Inout Mode): The names of the variables sent
to the function are matched with the parameter names in the
function. Thus, any change in the parameter changes the value
of the actual variable corresponding to that parameter.
Logical Programming