Finish Compilation Overview - Syntax




CS152

Chris Pollett

Sep 1, 2021

Outline

Introduction

Semantic Analysis and Symbol Tables

Runtime versus Compile-time semantic rules

Abstract Syntax Trees

Abstract Syntax Tree for GCD Program

Target Code Generation

Abstract Syntax Tree for GCD Program

Code Improvement

Syntax in Detail

Tokens and Regular Expressions

Regular Expression Example

In-Class Exercise

More on Tokens and Regular Expressions

Introduction to C and Unix Environment

Unix Environment Remarks

More Unix

For those of you unfamiliar with Unix we next look at some of the most important shell concepts/commands to get started with:

Still more Unix

A Basic C Program: hello.c

#include <stdio.h> //for printf statement
int main()
{
   printf("Hello World\n");
   return 0;
}

To compile and run this program:

gcc hello.c -o hello
./hello 

Remarks on hello.c

Similarities b/w Java and C

Important differences

You don't have a class construct. You can pretend --if it helps -- as if everything is declared in the same class. Although, these rules were relaxed somewhat in C99 from ANSI C, you should have all your variable declarations at the start of your functions:

void foo()
{
	int a=1;
    printf("%d", a);
    int b; // ANSI C's head explodes
}

Similarly, you either need to have a function prototype, or the function itself before the first place you use it -- See next slide.

Function examples

//the following is okay:
char foo();
void bar()
{
     printf("%c", foo());
}
char foo( )
{
     return 'a';
}