CPSC-323: Introduction and Phases of Compilers

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 6:36 AM on 10/1/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

31 Terms

1
New cards

Compiler

• A program that translates source code

written in a high-level language (like C, Java) into machine

code (low-level language) that a computer can execute.

• It performs this translation before the program runs.

• Output is usually executable code or intermediate code.

  • Ensures code is correct and safe to run


<p>• A program that translates source code</p><p>written in a high-level language (like C, Java) into machine</p><p>code (low-level language) that a computer can execute.</p><p>• It performs this translation before the program runs.</p><p>• Output is usually executable code or intermediate code.</p><ul><li><p>Ensures code is correct and safe to run</p></li></ul><p></p>
2
New cards

Benefits of a compiler

• Performance: Optimized machine code runs faster than interpreted

code.

• Early Error Detection: Compile-time errors catch bugs before

execution.

• Security: Invalid or malicious patterns can be caught during compilation.

• Portability: Same source code can target different architectures by changing the compiler backend


3
New cards

Compiler vs Interpreter

• Compiler: Translates entire code once, produces executable, faster runtime.

• Interpreter: Executes code line by line, slower but more flexible.

• Examples:

• Compiler: C, C++ programs.

• Interpreter: Python, JavaScript engines.

<p>• Compiler: Translates entire code once, produces executable, faster runtime.</p><p>• Interpreter: Executes code line by line, slower but more flexible.</p><p>• Examples:</p><p>• Compiler: C, C++ programs.</p><p>• Interpreter: Python, JavaScript engines.</p>
4
New cards

Another diagram of a compiler

knowt flashcard image
5
New cards

Lexical Analysis

  • 1st phase of a compiler

  • It reads the raw source code character-by-character and groups those characters into meaningful units called lexemes, then converts those lexemes into tokens.

  • If the compiler were a pipeline, this is the part that turns the messy text you typed into clean, structured data that the rest of the compiler can understand.


6
New cards

Why is Lexical Analysis needed?

• The parser can’t work directly on raw characters.

• Code needs to be broken into logical “words” before grammar rules

can apply.

• Errors like illegal characters must be caught early.

• Spaces, comments, and formatting must be ignored.

• Lexical analysis extracts meaning out of a stream of characters.

7
New cards

Lexeme

• The actual sequence of characters pulled directly from the source code.

• Think of it as: The raw “word” the programmer typed, A concrete substring taken from the character stream

• Examples: x, 123, while, +, "Hello"

• These are literal pieces of text.

8
New cards

Token

• The category or classification that a lexeme belongs to.

• A label the compiler attaches to a lexeme.

• Examples:

  • ID - for variable names like x, count, sum.

  • NUMBER - for 123, 45.6

  • KEYWORD_WHILE or KEYWORD

  • OP_PLUS

  • STRING_LITERAL

• Is like saying: “This lexeme belongs to this grammatical class.”

9
New cards

Lexical Analyzer Tasks

• Reads the input as a continuous stream of characters.

  • Example: int sum = a + 10;

• Groups characters into lexemes.

  • Examples: int, sum , = , a , + , 10 , ;

• Converts each lexeme into a token.

  • Examples: KEYWORD_INT, ID , ASSIGN , ID, PLUS, NUMBER, SEMICOLON

• Output:

  • KEYWORD_INT, ID(sum), ASSIGN, ID(a), PLUS, NUMBER(10), SEMICOLON


10
New cards

What are the tokens and lexemes for x = y + 5?

knowt flashcard image
11
New cards

What are the tokens and lexemes for int age = 21

knowt flashcard image
12
New cards

What are the lexemes and tokens for result = (a+b)*3

knowt flashcard image
13
New cards

What are the lexemes and tokens for while(count<10){ count=count+1; }?

knowt flashcard image
14
New cards

What are the lexemes and tokens for total = price * quantity / 2.5;

knowt flashcard image
15
New cards

Step 1 of a Compiler

Lexical Analysis

16
New cards

Step 2 of a Complier

Syntax Analysis/Parsing

17
New cards

Step 3 of a compiler

Semantic Analysis

18
New cards

Step 4 of a Compiler

Intermediate Code Generation

19
New cards

Step 5 of a Compiler

Code Optimization

20
New cards

Step 6 of a Compiler

Target Code Generation

21
New cards

ID

Token type for variable names

Ex:

  • x

  • count

  • sum


22
New cards

NUMBER

Token type for integers, floats

EX:

  • 123

  • 45.6


23
New cards

KEYWORD

Token type for reserved words

  • EX:

    • if

    • else

    • while

  • Another way to write the token type is KEYWORD_{insert keyword here}


24
New cards

OP

A token type representing operators

EX:

  • +

  • -

  • *

  • The tokens of these would be written as {}_PLUS, {}_MINUS, and {}_MUL


25
New cards

STRING_LITERAL

Token type for strings

EX:

  • “Hello World”


26
New cards

ASSIGN

Token type for =


27
New cards

SEMICOLON

Token type for a ;

28
New cards

L/RPAREN

Token type for (,)

29
New cards

L/RBRACE

Token type for {,}

30
New cards

FLOAT_LITERAL

Token type for decimals

EX:

  • 2.5


31
New cards

Alternative token expression for operators

  • +: PLUS

  • -: MINUS

  • *:MULTIPLY