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
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.