![Page 1: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/1.jpg)
Lexical and Syntax Analysis(of Programming Languages)
Abstract Syntax
![Page 2: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/2.jpg)
Lexical and Syntax Analysis(of Programming Languages)
Abstract Syntax
![Page 3: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/3.jpg)
What is Parsing?
String ofcharacters
Easy for humansto write
Easy for programsto process
Parser
A parser also checks that the input stringis well-formed, and if not, rejects it.
Data structure
![Page 4: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/4.jpg)
What is Parsing?
String ofcharacters
Easy for humansto write
Easy for programsto process
Parser
A parser also checks that the input stringis well-formed, and if not, rejects it.
Data structure
![Page 5: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/5.jpg)
Example 1
Charlton, 49
Lineker, 48
Beckham, 17
CSV (Comma Separated Value)
Array of pairs
Parser
“Charlton”
49
“Lineker”
48
“Beckham”
17
![Page 6: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/6.jpg)
Example 1
Charlton, 49
Lineker, 48
Beckham, 17
CSV (Comma Separated Value)
Array of pairs
Parser
“Charlton”
49
“Lineker”
48
“Beckham”
17
![Page 7: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/7.jpg)
Concrete andAbstract Syntax
The concrete syntax is a set ofrules that describe valid inputsto the parser.
The abstract syntax is a set ofrules that describe valid outputsfrom the parser.
The data structure produced bya parser is commonly termedthe abstract syntax tree.
![Page 8: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/8.jpg)
Concrete andAbstract Syntax
The concrete syntax is a set ofrules that describe valid inputsto the parser.
The abstract syntax is a set ofrules that describe valid outputsfrom the parser.
The data structure produced bya parser is commonly termedthe abstract syntax tree.
![Page 9: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/9.jpg)
Concrete andAbstract Syntax
String ofcharacters
Conforms to theConcrete Syntaxof the language
Conforms to theAbstract Syntaxof the language
Parser
Data structure
![Page 10: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/10.jpg)
Concrete andAbstract Syntax
String ofcharacters
Conforms to theConcrete Syntaxof the language
Conforms to theAbstract Syntaxof the language
Parser
Data structure
![Page 11: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/11.jpg)
Abstract syntax
The abstract syntax is usuallyspecified as a data type in theprogramming language beingused, in our case C. Example:
typedef struct {char* name;int goals;
} Player;
typedef struct {Player* players;int size;
} Squad;
An abstract syntax tree is avalue of this type.
![Page 12: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/12.jpg)
Abstract syntax
The abstract syntax is usuallyspecified as a data type in theprogramming language beingused, in our case C. Example:
typedef struct {char* name;int goals;
} Player;
typedef struct {Player* players;int size;
} Squad;
An abstract syntax tree is avalue of this type.
![Page 13: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/13.jpg)
This Chapter
How:
to define the abstract syntax
to construct abstract syntax trees
in the programming language C.
Also revisits some important Cprogramming techniques.
If you need a C tutorial then thefollowing books are recommended.
![Page 14: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/14.jpg)
This Chapter
How:
to define the abstract syntax
to construct abstract syntax trees
in the programming language C.
Also revisits some important Cprogramming techniques.
If you need a C tutorial then thefollowing books are recommended.
![Page 15: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/15.jpg)
POINTERS
Pointer: a variable that holds the address of a core storage location. [The Free Dictionary]
![Page 16: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/16.jpg)
POINTERS
Pointer: a variable that holds the address of a core storage location. [The Free Dictionary]
![Page 17: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/17.jpg)
Pointers
Declare a variable x of type intand initialise it to the value 10.
int x = 10; 10
x:
int* p;p:
p = &x;p:
10
x:
Declare a variable p of type int*(read: int pointer).
Make p point to x (or assign theaddress of x to p).
![Page 18: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/18.jpg)
Pointers
Declare a variable x of type intand initialise it to the value 10.
int x = 10; 10
x:
int* p;p:
p = &x;p:
10
x:
Declare a variable p of type int*(read: int pointer).
Make p point to x (or assign theaddress of x to p).
![Page 19: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/19.jpg)
Pointers
Print p (here, the address of x).
printf("%i\n", p );p:
10
x:
Print the value pointed to by p(here, the value of x).
printf("%i\n", *p );p:
10
x:
Assign 20 to the location pointedto by p.
*p = 20;p:
20
x:
![Page 20: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/20.jpg)
Pointers
Print p (here, the address of x).
printf("%i\n", p );p:
10
x:
Print the value pointed to by p(here, the value of x).
printf("%i\n", *p );p:
10
x:
Assign 20 to the location pointedto by p.
*p = 20;p:
20
x:
![Page 21: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/21.jpg)
Exercise 1
void swap(int* x, int* y) {
int tmp;tmp = *x;*x = *y;*y = tmp;
}
void main(){
int a = 1;int b = 2;swap(&a, &b);printf("a=%i, b=%i\n", a, b);
}
What is printed by the followingprogram?
![Page 22: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/22.jpg)
Exercise 1
void swap(int* x, int* y) {
int tmp;tmp = *x;*x = *y;*y = tmp;
}
void main(){
int a = 1;int b = 2;swap(&a, &b);printf("a=%i, b=%i\n", a, b);
}
What is printed by the followingprogram?
![Page 23: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/23.jpg)
DYNAMIC ALLOCATION
Dynamic Allocation: the allocation of memory storage for use in a computer program. [The Free Dictionary]
![Page 24: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/24.jpg)
DYNAMIC ALLOCATION
Dynamic Allocation: the allocation of memory storage for use in a computer program. [The Free Dictionary]
![Page 25: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/25.jpg)
Array allocation
int* p;p:
p = malloc(4 * sizeof(int));
p:
Declare a variable p of type int*.
Allocate memory for an array of4 int values and let point p to it.
![Page 26: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/26.jpg)
Array allocation
int* p;p:
p = malloc(4 * sizeof(int));
p:
Declare a variable p of type int*.
Allocate memory for an array of4 int values and let point p to it.
![Page 27: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/27.jpg)
Array indexing
*p = 10;p:
10
Assign 10 to the location pointedto by p.
p[0] = 20;p:
20
Assign 20 to the first element ofthe array pointed to by p.
p[2] = p[0];p:
20 20
Copy the first element of thearray to the third element.
![Page 28: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/28.jpg)
Array indexing
*p = 10;p:
10
Assign 10 to the location pointedto by p.
p[0] = 20;p:
20
Assign 20 to the first element ofthe array pointed to by p.
p[2] = p[0];p:
20 20
Copy the first element of thearray to the third element.
![Page 29: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/29.jpg)
Array deallocation
free(p);p:
When finished with an arrayallocated by malloc, call free torelease the space, otherwiseyour program may run out ofmemory.
Space released, so it canbe reused by future callsto malloc.
![Page 30: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/30.jpg)
Array deallocation
free(p);p:
When finished with an arrayallocated by malloc, call free torelease the space, otherwiseyour program may run out ofmemory.
Space released, so it canbe reused by future callsto malloc.
![Page 31: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/31.jpg)
STRINGS
String: a series of consecutive characters. [The Free Dictionary]
![Page 32: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/32.jpg)
STRINGS
String: a series of consecutive characters. [The Free Dictionary]
![Page 33: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/33.jpg)
Strings
char* s = “hi”;s:
h i \0
Declare a variable s, initialised topoint to the string “hi”.
s = s + 1;
Let s point to the next character.
s = s - 1;
And let s point to the previouscharacter again.
s:
h i \0
s:
h i \0
![Page 34: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/34.jpg)
Strings
char* s = “hi”;s:
h i \0
Declare a variable s, initialised topoint to the string “hi”.
s = s + 1;
Let s point to the next character.
s = s - 1;
And let s point to the previouscharacter again.
s:
h i \0
s:
h i \0
![Page 35: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/35.jpg)
Exercise 2
int f(char* s){
int i = 0;while (s[i] != '\0') i++;return i;
}
void main(){
char* x = “Hello”;printf(“%i\n”, f(x));
}
What is printed by the followingprogram?
![Page 36: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/36.jpg)
Exercise 2
int f(char* s){
int i = 0;while (s[i] != '\0') i++;return i;
}
void main(){
char* x = “Hello”;printf(“%i\n”, f(x));
}
What is printed by the followingprogram?
![Page 37: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/37.jpg)
USER-DEFINED TYPES
Type: the general character or structure held in common by a number of things. [The Free Dictionary]
![Page 38: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/38.jpg)
USER-DEFINED TYPES
Type: the general character or structure held in common by a number of things. [The Free Dictionary]
![Page 39: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/39.jpg)
Type definitions
typedef int Integer;
A typedef declaration allows anew name to be given to a type.
typedef char* String;
A new nameExisting type
String s; /* Declare a string s */Integer i; /* and an integer i */i = 0;s = “hello”;
Example use:
![Page 40: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/40.jpg)
Type definitions
typedef int Integer;
A typedef declaration allows anew name to be given to a type.
typedef char* String;
A new nameExisting type
String s; /* Declare a string s */Integer i; /* and an integer i */i = 0;s = “hello”;
Example use:
![Page 41: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/41.jpg)
Enumerations
enum colour {RED, GREEN, BLUE};
An enum declaration introducesa new type whose values aremembers of a given set.
Example use:
New type Possible values
enum colour c;c = RED;if (c == RED) printf(“Red\n”);
typedef enum colour Colour;
Give it a shorter name:
![Page 42: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/42.jpg)
Enumerations
enum colour {RED, GREEN, BLUE};
An enum declaration introducesa new type whose values aremembers of a given set.
Example use:
New type Possible values
enum colour c;c = RED;if (c == RED) printf(“Red\n”);
typedef enum colour Colour;
Give it a shorter name:
![Page 43: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/43.jpg)
Structures
An struct declaration introducesa new type that is a conjunctionof one or more existing types.
struct rectangle {float width;float height;
};
A width anda height
New type
struct rectangle r;r.width = 10;r.height = 20;
Example use:
struct circle {float radius;
}
A circle:
![Page 44: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/44.jpg)
Structures
An struct declaration introducesa new type that is a conjunctionof one or more existing types.
struct rectangle {float width;float height;
};
A width anda height
New type
struct rectangle r;r.width = 10;r.height = 20;
Example use:
struct circle {float radius;
}
A circle:
![Page 45: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/45.jpg)
Unions
An union declaration introducesa new type that is a disjunctionof one or more existing types.
union shape {struct circle circ;struct rectangle rect;
};
A circle ora rectangle
New type
struct shape s;s.circ.radius = 10;
Example use:
![Page 46: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/46.jpg)
Unions
An union declaration introducesa new type that is a disjunctionof one or more existing types.
union shape {struct circle circ;struct rectangle rect;
};
A circle ora rectangle
New type
struct shape s;s.circ.radius = 10;
Example use:
![Page 47: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/47.jpg)
Tagged unions
Often a tag is used to denote theactive disjunct of a union.
Another definition of shape:
struct shape {enum { CIRCLE, RECTANGLE } tag;union {
struct circle circ;struct rectangle rect;
};};
![Page 48: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/48.jpg)
Tagged unions
Often a tag is used to denote theactive disjunct of a union.
Another definition of shape:
struct shape {enum { CIRCLE, RECTANGLE } tag;union {
struct circle circ;struct rectangle rect;
};};
![Page 49: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/49.jpg)
Tagged unions
struct shape s, t;
s.tag = CIRCLE;s.circ.radius = 10;
t.tag = RECTANGLE;t.rect.width = 5;t.rect.height = 15;
Example: s is a circle and t is arectangle, and both are of typestruct shape.
![Page 50: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/50.jpg)
Tagged unions
struct shape s, t;
s.tag = CIRCLE;s.circ.radius = 10;
t.tag = RECTANGLE;t.rect.width = 5;t.rect.height = 15;
Example: s is a circle and t is arectangle, and both are of typestruct shape.
![Page 51: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/51.jpg)
Tagged unions
Example: compute the area ofany given shape s.
float area(struct shape s){
if (s.tag == CIRCLE) {float r = s.circ.radius;return (3.14 * r * r);
}if (s.tag == RECTANGLE) {
return (s.rect.width *s.rect.height);
}}
![Page 52: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/52.jpg)
Tagged unions
Example: compute the area ofany given shape s.
float area(struct shape s){
if (s.tag == CIRCLE) {float r = s.circ.radius;return (3.14 * r * r);
}if (s.tag == RECTANGLE) {
return (s.rect.width *s.rect.height);
}}
![Page 53: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/53.jpg)
Recursive structures
A value of type struct t maycontain a value of type struct t*.
struct list {int head;struct list* tail;
};
typedef struct list List;
(*xs).head ≡ xs->head
Suppose x is a value of type List*.
(*(*xs).tail).head ≡ xs->tail->head
![Page 54: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/54.jpg)
Recursive structures
A value of type struct t maycontain a value of type struct t*.
struct list {int head;struct list* tail;
};
typedef struct list List;
(*xs).head ≡ xs->head
Suppose x is a value of type List*.
(*(*xs).tail).head ≡ xs->tail->head
![Page 55: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/55.jpg)
Recursive structures
Example: inserting an item ontothe front of a linked list.
List* insert(List* xs, int x){
List* ys = malloc(sizeof(List));ys->head = x;ys->tail = xs;return ys;
}
![Page 56: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/56.jpg)
Recursive structures
Example: inserting an item ontothe front of a linked list.
List* insert(List* xs, int x){
List* ys = malloc(sizeof(List));ys->head = x;ys->tail = xs;return ys;
}
![Page 57: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/57.jpg)
CASE STUDY
A simplifier for arithmetic expressions.
![Page 58: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/58.jpg)
CASE STUDY
A simplifier for arithmetic expressions.
![Page 59: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/59.jpg)
Concrete syntax
Consider the following concretesyntax for arithmetic expressions,where v ranges over variablenames and n over integers.
e → v| n| e + e| e * e| ( e )
Example expression:
x * y + (z * 10)
![Page 60: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/60.jpg)
Concrete syntax
Consider the following concretesyntax for arithmetic expressions,where v ranges over variablenames and n over integers.
e → v| n| e + e| e * e| ( e )
Example expression:
x * y + (z * 10)
![Page 61: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/61.jpg)
Simplification
Consider the algebraic law:
∀x. x * 1 = x
Example simplification:
x * (y * 1) → x * y
This law can be used to simplifyexpressions by using it as arewrite rule from left to right.
![Page 62: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/62.jpg)
Simplification
Consider the algebraic law:
∀x. x * 1 = x
Example simplification:
x * (y * 1) → x * y
This law can be used to simplifyexpressions by using it as arewrite rule from left to right.
![Page 63: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/63.jpg)
Problem
1. Define an abstract syntax, in C,for arithmetic expressions.
2. Show how to construct abstractsyntax trees that representarithmetic expressions.
3. Implement the simplification as aC function that takes and returnsan abstract syntax tree.
![Page 64: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/64.jpg)
Problem
1. Define an abstract syntax, in C,for arithmetic expressions.
2. Show how to construct abstractsyntax trees that representarithmetic expressions.
3. Implement the simplification as aC function that takes and returnsan abstract syntax tree.
![Page 65: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/65.jpg)
Abstract syntax
typedef enum { ADD, MUL } Op;
struct expr {enum { VAR, NUM, APP } tag;union {
char* var;int num;struct {
struct expr* e1;Op op;struct expr* e2;
} app;};
};
typedef struct expr Expr;
A variable ora number oran op and twosub-expressions
![Page 66: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/66.jpg)
Abstract syntax
typedef enum { ADD, MUL } Op;
struct expr {enum { VAR, NUM, APP } tag;union {
char* var;int num;struct {
struct expr* e1;Op op;struct expr* e2;
} app;};
};
typedef struct expr Expr;
A variable ora number oran op and twosub-expressions
![Page 67: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/67.jpg)
Constructors
Expr* mkVar(char* v) {Expr* e = malloc(sizeof(Expr));e->tag = VAR; e->var = v;return e;
}
Expr* mkNum(int n) {Expr* e = malloc(sizeof(Expr));e->tag = NUM; e->num = n;return e;
}
Expr* mkApp(Expr* e1, Op op, Expr* e2) {Expr* e = malloc(sizeof(Expr));e->tag = APP; e->app.op = op;e->app.e1 = e1; e->app.e2 = e2;return e;
}
![Page 68: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/68.jpg)
Constructors
Expr* mkVar(char* v) {Expr* e = malloc(sizeof(Expr));e->tag = VAR; e->var = v;return e;
}
Expr* mkNum(int n) {Expr* e = malloc(sizeof(Expr));e->tag = NUM; e->num = n;return e;
}
Expr* mkApp(Expr* e1, Op op, Expr* e2) {Expr* e = malloc(sizeof(Expr));e->tag = APP; e->app.op = op;e->app.e1 = e1; e->app.e2 = e2;return e;
}
![Page 69: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/69.jpg)
Abstract syntax trees
mkApp( mkVar("x"), ADD, mkApp( mkVar("y")
, MUL, mkNum(2)))
An abstract syntax tree thatrepresents the expression
x + y * 2
can be constructed by thefollowing C expression
![Page 70: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/70.jpg)
Abstract syntax trees
mkApp( mkVar("x"), ADD, mkApp( mkVar("y")
, MUL, mkNum(2)))
An abstract syntax tree thatrepresents the expression
x + y * 2
can be constructed by thefollowing C expression
![Page 71: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/71.jpg)
Simplification
void simplify(Expr* e){
if (e->tag == APP&& e->app.op == MUL&& e->app.e2->tag == NUM&& e->app.e2->num == 1) {
*e = *(e->app.e1);}if (e->tag == APP) {
simplify(e->app.e1);simplify(e->app.e2);
}}
∀x. x * 1 = x
is implemented by
![Page 72: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/72.jpg)
Simplification
void simplify(Expr* e){
if (e->tag == APP&& e->app.op == MUL&& e->app.e2->tag == NUM&& e->app.e2->num == 1) {
*e = *(e->app.e1);}if (e->tag == APP) {
simplify(e->app.e1);simplify(e->app.e2);
}}
∀x. x * 1 = x
is implemented by
![Page 73: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/73.jpg)
Homework exercises
Extend the simplifier to exploitthe following algebraic law.
Implement a pretty printer thatprints an abstract syntax tree ina concrete form.
void print(Expr* e){
...}
∀x. x * 0 = 0
![Page 74: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/74.jpg)
Homework exercises
Extend the simplifier to exploitthe following algebraic law.
Implement a pretty printer thatprints an abstract syntax tree ina concrete form.
void print(Expr* e){
...}
∀x. x * 0 = 0
![Page 75: Lexical and Syntax Analysis - University of YorkConcrete and Abstract Syntax The concrete syntax is a set of rules that describe valid inputs to the parser. The abstract syntax is](https://reader036.vdocuments.us/reader036/viewer/2022081400/5f16d4063286fc0ecc609567/html5/thumbnails/75.jpg)
Motivation for LSA
In LSA, we are interested in howto implement the following kindof function
Expr* parse(char* string){
...}
It takes a string conforming tothe concrete syntax and returnsan abstract syntax tree.