Compiler Design
Presentation
Prepared by: Aniket Mali (12202040501007)
Daiwik Korat (12202040501012)
Dhyan Shah (12202040501023)
Academic Year: 2024-25
Batch: 1A04
Mini programming
Language parser using
Lex & Yacc
Introduction
 What is a Parser?
• A parser analyzes source code and checks if it follows language
grammar.
 Why use Lex & Yacc?
• Lex: Generates scanner (tokenizer).
• Yacc: Generates parser (syntax analyzer).
 Mini Programming Language Concept:
• A simplified C-like language designed for learning compiler
construction.
• Easier to implement but models real-world compiler behavior.
Motivation
3
 Why Compiler Construction?
• Every programming language needs a compiler/interpreter.
• Understanding compilers helps in language design and optimization.
 Importance of Scanners & Parsers:
• Scanner (Lex): Breaks raw input into tokens (words with meaning).
• Parser (Yacc): Ensures tokens follow the grammar rules.
 Why Mini-Languages?
• Easier to design and understand.
• A stepping stone towards building real compilers.
Role of Lex and Yacc
4
 Lex:
• Converts input source code into a sequence of tokens.
• Uses regular expressions to define patterns.
 Yacc:
• Uses context-free grammar to validate syntax.
• Builds parse tree for semantic meaning.
 Workflow:
• Source Code → Lex (Scanner) → Tokens → Yacc (Parser) → Parse Tree
Problem Objectives
5
• Design and implement a parser for a C-like mini language.
 Features supported:
• Declarations: int x;, float y;
• Assignments: x = 5;, y = 2.5 * 3.0;
• If-Else Statements: if (x > 0) x = x - 1;
• Switch-Case Constructs:
• Return Statements: return x;
Language Features
6
 Data Types:
• int → integer variables.
• float → floating-point variables.
 Control Flow:
• if, else for decisions.
• switch, case, default for multiple choices.
• return for function exit.
 Identifiers:
• User-defined variable names like x, y, sum.
 Operators:
• Arithmetic: +, -, *
• Assignment: =
• Relational: >, <, ==
• Delimiters: ;, {, }, (, )
 Constants:
• Integers (NUM) → e.g., 10, 25
• Floating-point (FLOAT_NUM) → e.g., 3.14, 2.0
7
 Purpose: Define lexical tokens using regex.
 Token Categories:
• Keywords → int, float, if, else, switch, return, case, default.
• Identifiers → [a-zA-Z][a-zA-Z0-9]*
• Numbers → Integers [0-9]+, Floats [0-9]+.[0-9]+
 Example Mapping:
• "int" → INT
• "float" → FLOAT
• 123 → NUM
• 3.14 → FLOAT_NUM
Lex(Scanner) Overview
8
 Purpose:
• Check if sequence of tokens follows grammar.
• Define syntax in CFG style.
 Rules Example:
• decl → type ID ;
• assign → ID = expr ;
• if_stmt → IF ( cond ) stmt [ELSE stmt]
• switch_stmt → SWITCH (ID) { case_list }
• return_stmt → RETURN expr ;
 Output:
• Produces Parse Tree if syntax is valid.
Yacc(Parser) overview
9
Lex Code Snippet
10
Yacc Code Snippet
Yacc Code Snippet
11
Error Handling
12
 Examples of Errors:
• int ; → Error: missing identifier.
• x = ; → Error: missing expression.
• switch { case: } → Error: missing condition.
 Error Recovery in Yacc:
• Use special error token to capture invalid input.
• Allows parser to continue instead of terminating.
Example run
• Input:
• Output:
Applications
14
 Education:
• Teaching compiler construction concepts.
 Programming Tools:
• Syntax checking in IDEs.
• Custom interpreters for small languages.
 Industry:
• Designing Domain-Specific Languages (DSLs).
• Validation of config files or query languages.
 Research:
• Foundation for building optimized compilers.
15
 Conclusion:
• Lex + Yacc simplify compiler front-end design.
• Mini programming language parser successfully implemented.
 Supports: Declarations, Assignments, If-Else, Switch-Case, Return, Integers & Floats.
 Future Enhancements:
• Loops (while, for).
• Functions & parameter passing.
• Arrays and pointers.
• Error diagnostics and semantic checks.
Conclusion & Future Scope
16
Thank
You!

Parsing_using)_lex_and_yacc_compilerdesign.pptx

  • 1.
    Compiler Design Presentation Prepared by:Aniket Mali (12202040501007) Daiwik Korat (12202040501012) Dhyan Shah (12202040501023) Academic Year: 2024-25 Batch: 1A04 Mini programming Language parser using Lex & Yacc
  • 2.
    Introduction  What isa Parser? • A parser analyzes source code and checks if it follows language grammar.  Why use Lex & Yacc? • Lex: Generates scanner (tokenizer). • Yacc: Generates parser (syntax analyzer).  Mini Programming Language Concept: • A simplified C-like language designed for learning compiler construction. • Easier to implement but models real-world compiler behavior.
  • 3.
    Motivation 3  Why CompilerConstruction? • Every programming language needs a compiler/interpreter. • Understanding compilers helps in language design and optimization.  Importance of Scanners & Parsers: • Scanner (Lex): Breaks raw input into tokens (words with meaning). • Parser (Yacc): Ensures tokens follow the grammar rules.  Why Mini-Languages? • Easier to design and understand. • A stepping stone towards building real compilers.
  • 4.
    Role of Lexand Yacc 4  Lex: • Converts input source code into a sequence of tokens. • Uses regular expressions to define patterns.  Yacc: • Uses context-free grammar to validate syntax. • Builds parse tree for semantic meaning.  Workflow: • Source Code → Lex (Scanner) → Tokens → Yacc (Parser) → Parse Tree
  • 5.
    Problem Objectives 5 • Designand implement a parser for a C-like mini language.  Features supported: • Declarations: int x;, float y; • Assignments: x = 5;, y = 2.5 * 3.0; • If-Else Statements: if (x > 0) x = x - 1; • Switch-Case Constructs: • Return Statements: return x;
  • 6.
    Language Features 6  DataTypes: • int → integer variables. • float → floating-point variables.  Control Flow: • if, else for decisions. • switch, case, default for multiple choices. • return for function exit.  Identifiers: • User-defined variable names like x, y, sum.  Operators: • Arithmetic: +, -, * • Assignment: = • Relational: >, <, == • Delimiters: ;, {, }, (, )  Constants: • Integers (NUM) → e.g., 10, 25 • Floating-point (FLOAT_NUM) → e.g., 3.14, 2.0
  • 7.
    7  Purpose: Definelexical tokens using regex.  Token Categories: • Keywords → int, float, if, else, switch, return, case, default. • Identifiers → [a-zA-Z][a-zA-Z0-9]* • Numbers → Integers [0-9]+, Floats [0-9]+.[0-9]+  Example Mapping: • "int" → INT • "float" → FLOAT • 123 → NUM • 3.14 → FLOAT_NUM Lex(Scanner) Overview
  • 8.
    8  Purpose: • Checkif sequence of tokens follows grammar. • Define syntax in CFG style.  Rules Example: • decl → type ID ; • assign → ID = expr ; • if_stmt → IF ( cond ) stmt [ELSE stmt] • switch_stmt → SWITCH (ID) { case_list } • return_stmt → RETURN expr ;  Output: • Produces Parse Tree if syntax is valid. Yacc(Parser) overview
  • 9.
  • 10.
  • 11.
  • 12.
    Error Handling 12  Examplesof Errors: • int ; → Error: missing identifier. • x = ; → Error: missing expression. • switch { case: } → Error: missing condition.  Error Recovery in Yacc: • Use special error token to capture invalid input. • Allows parser to continue instead of terminating.
  • 13.
  • 14.
    Applications 14  Education: • Teachingcompiler construction concepts.  Programming Tools: • Syntax checking in IDEs. • Custom interpreters for small languages.  Industry: • Designing Domain-Specific Languages (DSLs). • Validation of config files or query languages.  Research: • Foundation for building optimized compilers.
  • 15.
    15  Conclusion: • Lex+ Yacc simplify compiler front-end design. • Mini programming language parser successfully implemented.  Supports: Declarations, Assignments, If-Else, Switch-Case, Return, Integers & Floats.  Future Enhancements: • Loops (while, for). • Functions & parameter passing. • Arrays and pointers. • Error diagnostics and semantic checks. Conclusion & Future Scope
  • 16.