Download presentation
Presentation is loading. Please wait.
1
Informal Compiler Algorithm Notation
(ICAN)
2
Extended Backus-Naur Form (XBNF)
Used to describe the syntax of Programming Languages. Notations & Symbols 1. Terminals - typewriter fonts “type” 2. Nonterminals – italic font “ProgUnit” 3. Production nonterminal → sequence of nonterminals, terminals and operators 4.”Є” - empty string
3
XBNF Symbol Meaning I Separates alternatives {and} Grouping [and]
Optional * Zero or more repetitions + One or more repetition ⋈ One or more repetitions of the left operand separated by occurrences of the right operand
4
ArrayTypeExpr → array [ArrayBounds] of TypeExpr
ArrayBounds → {[Expr] .. [Expr]} ⋈ , array [ . .] of integer array [ ] of real array [ 1. .2, 1. .2] of real
5
Introduction to ICAN ICAN derives features from several programming languages such as C, Pascal, Modula-2, and that extends them with natural notations for objects such as sets, tuples, sequences, functions, arrays, and compiler specific types.
6
Struc: Node → set of Node
Procedure Example_1(N,r) N: in set of Node r: in Node begin change := true: boolean D, t: set of Node n, p: Node Struc(r) := {r} for each n є N (n ≠ r) do Struc(n) := N od while change do change := false for each n є N – {r} do t := N for each p є Pred[n] do t ∩= Struc(p) D := {n} υ t if D ≠ Struc(n) then change := true; Struc(n) := D fi end || Example_1 A sample ICAN global declaration and procedure
7
Overview of ICAN ICAN program consists of Series of type definition
Followed by series of variable declarations Followed by series of procedure declarations Followed by an optional main program
8
Type Definition It consists of type name followed by an equals sign and a type expression, intset = set of integer Types may be either generic or compiler-specific, and either simple or compound.
9
boolean Integer real Character The generic simple types are
The type constructors are Constructor Name Example Description enum Enumeration enum {left, rigt} array ... of Array array [1..10] of integer set of Set set of MIRInst sequence of Sequence sequence of boolean x Tuple integer x set of real record Record record {x: real, y: real} υ Union integer υ boolean → Function integer → set of real
10
Variable Declaration e.g., is := {1,3,7}: intset
Variable declaration consists of name of the variable, followed by an optional initialization, followed by a colon and the variable’s type e.g., is := {1,3,7}: intset
11
Procedural Declaration
It consists of Procedure’s Name Parameter list in parenthesis Optional return type Parameter declaration Body Procedure exam(x,y,is) returns boolean x,y: out integer is: in integer Begin tv := true: boolean z: integer for each zє is (z>0) do if x = z then return tv fi od return y є is end || exam
12
Expressions Constant A variable nil
Unary operator followed by an expression An array expression A sequence expression A set expression A tuple expression A record expression A procedure or function call An array element A tuple element A record field A quantified expression
13
Examples of Constants of constructed type
Example Constant array [1..2, 1..2] of real [[1.0,2.0],[3.0,4.0]] sequence of integer [2,3,5,7,9,11] integer x boolean <3, true> set of (integer x real) {<3,3.0>, <2,2.0>} record {x: real, y:real} <x:1.0, y:-1.0> (integer x real)→ boolean {<1,2.0,true>,<1,3.0,false>}
14
Operator appropriate to specific types
Empty set Ø Empty sequence [ ] Value of any uninitialized variable nil Size of any constructed type, x Cardinality for sets Length of sequence |x| Unary operator, returns the arbitrary member of the set ♦ ith member of the sequence sq sq ↓ i Concatenation of the sequences sq1, sq2 sq1 ⊕ sq2 sq with ith element removed sq Өi ith element of tuple tpl i
15
Beginning, internal and ending delimiter of the Compound Statement
if elif, else fi case of, default esac for do od while repeat until
16
ICAN Program It consists of Series of type definition
Series of variable declarations Series of procedural declarations Optional main program Main program have the form of a procedure body
17
IntPair = integer υ (IntPair x IntPair)
Type Definition A type definition consists of one or more pairs of a type name followed by an equal sign “=“, followed by the definition. IntPair = integer υ (IntPair x IntPair) The set containing all integers and all pairs each of whose elements is either an integer or a pair of elements of the type
18
Syntax of ICAN type definition
TypeDef → {TypeName =}* TypeExpr TypeName → Identifier TypeExpr → SimpleTypeExpr | ConstrTypeExpr | (TypeExpr) | TypeName | Ø SimpleTypeExpr → boolean | integer | real | character ConstrTypeExpr → EnumTypeExpr | ArrayTypeExpr | SetTypeExpr | SequenceTypeExpr | TupleTypeExpr | RecordTypeExpr | UnionTypeExpr | FuncTypeExpr EnumTypeExpr → enum {Identifier ⋈ , } ArrayTypeExpr → {[Expr] .. [Expr]} ⋈ , SetTypeExpr → set of TypeExpr SequenceTypeExpr → sequence of TypeExpr TupleTypeExpr → TypeExpr ⋈ x RecordTypeExpr→ record{{identifier ⋈ , : TypeExpr} ⋈ ,} UnionTypeExpr→ TypeExpr ⋈ υ FuncTypeExpr → TypeExpr ⋈ x → TypeExpr TypeDef →
19
Declarations VarDec → {Variable [:= ConstExpr]} ⋈ , : TypeExpr Variable→ Identifier ProcDecl → procedure ProcName ParamList [return TypeExpr] ParamDecls ProcBody ProcName → Identifier ParamList→ ([Parameter ⋈ , ]) Parameter → Identifier ParamDecls → ParamDecl* ParamDecl → Variable ⋈ , : {in | out | inout} TypeExpr ProcBody → begin VarDecl* Statement* end
20
Data Types & Expressions
Expr → Variable | SimpleCost | (Expr) | UnaryOper Expr | Expr BinOper Expr | ArrayExpr | SequenceExpr | SetExpr | TupleExpr | RecordExpr | ProcFuncExpr | ArrayEltExpr| |SizeExpr | QuantExpr | Expr є TypeExpr | nil UnaryOper → ! | - | ♦ BinaryOper → = | ≠ | & | ⋁ | + | -| / | % | ∗ | ∪|∩| ↑ | ↓ | < | > | ≤| ≥ | Ө |∊ | x |⊕| . ArrayExpr→ [ Expr ⋈ ,] SequenceExpr → [ Expr ⋈ ,] | “ ASCII Character* ” SetExpr → Ø | {Expr ⋈ , | [Variable ∊ ] Expr where SetDefClause} TupleExpr→ 〈 Expr ⋈ , 〉 RecordExpr →〈 { Identifier : Expr} ⋈ ,〉 ProcFuncExpr→ ProcName ArgList ArgList → ([ Expr ⋈ ,]) ArrayEltExpr → Expr [Expr ⋈ ,] QuantExpr → {∀|∃} Variable ∈ [ Expr | TypeExpr] (Expr) Size → |Expr| Variable → Identifier
21
ICAN Constants of generic Simple Type
SimpleConstant → intConst|RealConst|BoolConst|CharConst IntConst→ 0|[-]NZDigit Digit* | [-] ∞ NZDigit →1|2|3|4|5|6|7|8|9 Digit → 0|NZDigit RealConst → [-] {IntConst .[ IntConst] | [IntConst] . IntConst} [ E IntConst] | [-] ∞ BoolConst → true | false CharConst → ‘ASCIICharacter’ Identifier → Letter{Letter|Digit|-}* Letter→ a |…| z | A|…|Z
22
Binary Operators Apply to Boolean Unary Operators Apply to Boolean
Symbol Equals = Not Equals ≠ Logical and & Logical or V Unary Operators Apply to Boolean ! Negation ∀ ,∃ Quantified operators Example : ∃v ∈ Var (Opnd(inst, v)) A quantified expression that evaluate to true if and only if there is some variable v such that Opnd(inst, v) is true
23
Operators apply to finite integers and reals:
Operation Symbol Plus + Minus - Times * Divide / Modulo % Exponentiation ↑ Equals = Not equals ≠ Less than < Less than or equal to ≤ Greater than > Greater than or equal to ≥
24
Enumerated Types A non-empty finite set of identifiers.
A variable var is declared to be of an enumerated type by a declaration of the form var : enum {element1, …, elementn} where each elementi is an identifier. Example: action : enum {Shift, Reduce, Accept, Error}
25
Array var: array [Sublist] of basetype U: array [5..8] of integer
Declaration: var: array [Sublist] of basetype Example: U: array [5..8] of integer V: array [1..2, 1..3] of real U := [1,2,3,4] V := [1.0, 2.0, 3.0][4.0,5.0,6.0]
26
Sets Set constants is either the empty
Set “ ∅ “, a comma separated series of elements enclosed by curly braces, or an intentionally defined set of constants Declaration: var: set of basetype Example: B: set of integer B:= {1, 4, 9, 16} B:= { n ∈ integer where ∃m ∈ integer (1≤ m & m≤ 4 & n = m ∗ m ) } Operation Symbol Union ∪ Intersection ∩ Difference − Product x Member of ∈ Not member of ∉
27
Set (Cont…) S := ∅ for each n ∈ N do for each e ∈ E do if e@2 = n then
S ∪= {n} fi od (b) Tmp := A while Tmp ≠ ∅ do a := ◆Tmp Tmp -= {a} body od (a) ICAN programs using for and while loops iterate over sets (a) & (b)
28
Sequences var: sequence of basetype
basetype is the element type of the sequence that may be var’s value A constant of sequence type is a finite comma-separated series of members of its base type enclosed in square bracket. Empty sequence is denoted by “[ ]” Example Sequence: [ ] [1] [1,2,3] [true, false]
29
Sequences(Cont…) Sequence operations & operands
[ 2, 3, 5, 7]↓2 = 3 ( returns the 2nd elements from the sequence) [2, 3, 5, 7] ↓-2 = 5 (return the 2nd element from the end of the sequence) [1, 2, 3] ⊕ [4, 5, 6] = [1, 2, 3, 4, 5, 6] (concatenation of two sequences) [1, 2, 3, 4]⊖2 = [1, 3, 4] ( remove the second element from the sequence) [1, 2, 3, 4, 5] -⊖ 2= [1, 2, 3, 5] (remove the 2nd elements from the end of the sequence)
30
Tuples var: basetype1 x basetype2 x…x basetypen Where basetypei is the type of the ith component A tuple constant is a fixed length comma separated series enclosed in angle brackets. Example: integer x integer x boolean 〈1,7,true〉 〈1,2, false〉 〈1, 2, true 3 = true
31
Record ibpair = record {int: integer, bool: boolean}
Record constant is tuple, each of whose elements is a pair consisting of an identifier (called the selector) and a value, separated by a colon. var: record {idents1:basetype1, …, identsn: basetypen} ibpair = record {int: integer, bool: boolean} 〈int:3, bool: true 〉 & 〈bool:true,int:3〉 are identical
32
Record ibpair = record{int: integer, bool: boolean} . . . B: boolean
ibp: ibpair ibp := 〈int:2, bool: true 〉 ibp .int := 3 b:= ibp.bool ibp and b are 〈int:3, bool: true 〉 and true , respectively
33
Union Example: integer U boolean
A union type is the union of the sets of values in the types that makes up the union A variable var is declared to be of a union type by a declaration of the form var : basetype1 U … U basetypen Example: integer U boolean An element of this type is either an integer or a Boolean All the operators that apply to sets apply to unions
34
Function var: basetype1 x … x basetypen → basetype0
A function type has a domain type (written left of the arrow) and a range type (written to the right of the arrow) var: basetype1 x … x basetypen → basetype0 A function constant with n argument positions is a set each of whose elements is an (n+1)th – tuple whose first n arguments are of the 1st through nth domain type respectively, and whose (n+1)th member is of the range type. Example: A: boolean → integer We would write, for example, either A := {〈true,3〉 , 〈false,2〉} or A(true) :=3 A(false):=2 to assign a particular function to be the value of A
35
Compiler-Specific Types
The compiler-specific types are all named with an uppercase initial letter and are introduced as needed. Example Block = array [..] of array [..] of MIRInst Procedure = Block Edge = Node x Node
36
nil nil is a member of every type.
The value of any variable that has not been inialized Expression Result nil = nil true a = nil false nil ≠ nil a≠ nil
37
The Size Operator The operator “| |“ applies to objects of all constructed types. In each case, its value is the number of elements in its argument. A: array [1..5, 1..5] of boolean and f is declared and defined to be f: integer x integer → boolean … f(1,1) := true f(1,2) := false f(3,4) := true then |{1, 7, 23}| =3 |[‘a’, ‘b’, ‘e’, ‘c’, ‘b’ ] | =5 |〈r1:1.0, im:-1.0〉| = 2 |A| =25 |f | =3
38
Assignment Statement A series of one or more left-hand parts, each followed by an assignment operator, and right-hand part Example: S := S1 U= {a} ⋂ X Is equivalent to S := S1 := S1 U ({a} ⋂ X) Which, in turn, is equivalent to S1 := S1 U ({a} ⋂ X) S := S1 Rather than to S1 := (S1 U ({a}) ⋂ X
39
Procedure Call Statement
It consists of procedure name followed by a parenthesized list of arguments separated by commas Return Statement Consists of keyword return Goto Statement Consists of keyword goto followed by a label.
40
If Statement if condition0 then then_body elif condition1 then
elif_body1 … elif conditionn then elif_bodyn else else_body fi
41
CASE Statement case selector of label1 : body1 label2: body2 …
labeln: bodyn Default: body0 esac
42
While Statements For statement Repeat statement while condition do
while_body od For statement for iterator do for_body Repeat statement repeat repeat_body until condition
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.