0% found this document useful (0 votes)
23 views2 pages

Cheat Sheet Updated

Cheey sheet

Uploaded by

G Y
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)
23 views2 pages

Cheat Sheet Updated

Cheey sheet

Uploaded by

G Y
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
You are on page 1/ 2

BNF Fundamentals -Lex builds the yylex() function that is called, and will do ((NULL? lis1) (NULL?

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

1.All parameters are passed by value.


35 26 25
5 10 15 --parameters are independent variables
initialized to the values of the argument expressions.
Changes to them do not effect the arguments.
2.Pass a and b by reference, and c by value. static link parent fonksiyona gider.dynamic link çağrıldığı
35 26 25 yere gider.
35 26 15 --This is very much same,except the changes to a (CHAIN_OFFSET, LOCAL_OFFSET)
and b are also made to i and j since these parameters are At position 1 in sub1:
aliases. A - (0,3)
3.Pass a and b by value-result, and c by value. B - (1,4)
35 26 25 C - (1,5)
35 26 15 --This has the same effect as pass-by-reference, At position 2 in sub3:
since the values of a and b are returned to i and j when E - (0,4)
the function returns. B - (1,4)
4.All parameters are passed by name. A - (2,3)
35 26 41 At position 3 in sub2:
35 26 15 --This is again similar to the last two,since A - (1,3) -parent(X,Y) :- mother(X,Y). >>> “X, Y’nin annesi ise X, Y’nin
changes to a and b also change i and j. The difference is D - error ebeveynidir.”
the value of 41 printed for c, since c is an alias for the E - (0,5) -grandparent(X,Z):- parent(X,Y), parent(Y,Z). >>> “Y, Z’nin
expression j+k. The assignment b := c + 1 changes b to 26, Dynamic chain (call chain): The collection of dynamic links ebeveyni ve X’de Y’nin ebeveyni ise X, Z’nin atasıdır.”
which changes j, which changes j+k, now 26+15. in the stack at a given time.
-Local variables can be accessed by their offset from the
beginning of the activation record, whose address is in
the EP. This offset is called the local_offset.
Implementing Static Scoping
-A static chain is a chain of static links that connects
certain activation record instances.
-The static link in an activation record instance for
subprogram A points to the bottom of one of the
activation record instances of A's static parent.
-The static chain from an activation record instance
connects it to all of its static ancestors.

-An overloaded subprogram is one that has the same


name as another subprogram in the same referencing -Static_depth is an integer associated with a static scope
environment. whose value is the depth of nesting => indicates how
-A generic or polymorphic subprogram takes parameters deeply it is nested in the outermost scope.
of different types on different activations. STATIC_DEPTH-> scope iç içe geme sırası. ağaçtaki
-A coroutine is a special kind of a subprogram that has derinliği.
multiple entries and controls them itself.Coroutines call is -The chain_offset or nesting_depth of a nonlocal
a resume. reference is the difference between the static_depth of
Implementing Simple Subprograms the reference and static_depth of the procedure
-Two separate parts: the actual code (constant) and the containing its declaration.
non-code part (local variables and data that can change). CHAIN_OFFSET-> variablenın declare edildiği yerin ilk
-The format, or layout, of the non-code part of an tanımlandığı parent scope a olan uzaklığı.
executing subprogram is called an activation record. LOCAL_OFFSET-> variablenın ilk olarak tanımladığı
-The form of an activation record is static. fonksiyonun ARI'sindeki alttan yukarıya sırası.0'dan
-An activation record instance (ARI) is a concrete example baslayıp.
of an activation record. -A reference to a variable can be represented by the pair:
(chain_offset, local_offset), where local_offset is the
offset in the activation record of the variable being
referenced.

You might also like