Professional Documents
Culture Documents
Project Report on
Compiler Design BTECCE21603
Project - I
Submitted to Vishwakarma University, Pune
Under the Initiative of
Contemporary Curriculum, Pedagogy, and Practice (C2P2)
By
Mohee Bansal
SRN No : 202101316
Roll No : 37
Div : A
Third Year Engineering
Academic Year
2023-2024
Compiler Design BTECCE21603
A. Design and implement a lexical analyzer for a Music Score Language (MSL) that allows
users to describe and notate musical compositions in a structured format. The primary goal
is to develop a robust system capable of parsing music score specifications, identifying
components such as notes, rests, chords, time signatures, key signatures, dynamics,
articulations, and other relevant details.
The lexical analyzer is the first phase of a compiler or interpreter, and its main
purpose is to break down the input text (the music score in this case) into smaller
units called tokens.
1. Tokenization:
The lexical analyzer would read the input music score language text and break
it down into tokens based on the rules of the music score language. Tokens in
a music score language might include:
2. Lexical Analysis:
The lexical analyzer would use regular expressions or other matching rules to
recognize these tokens. For example:
3. Token Output:
As the lexical analyzer identifies tokens, it would output them with their
corresponding types. For example:
4. Error Handling:
The lexical analyzer would also detect and report lexical errors, such as
unrecognized symbols or invalid combinations of symbols.
5. Passing to Parser:
Once the input music score text has been tokenized, the tokens are passed
on to the next phase of the compiler, the parser. The parser then uses these
tokens to build a syntax tree or perform further analysis based on the
grammar of the music score language.
iv. Code with output screen shots
JJ File code:
// MusicScoreLexer.jj
options {
STATIC = false;
}
PARSER_BEGIN(MusicScoreLexer)
import java.io.*;
import java.util.*;
public class MusicScoreLexer {
public static void main(String[] args) throws ParseException, IOException {
MusicScoreLexer lexer = new MusicScoreLexer(new
FileInputStream(args[0]));
Token token;
while ((token = lexer.getNextToken()).kind != MusicScoreLexerConstants.EOF)
{
if (token.kind == MusicScoreLexerConstants.OTHER_SYMBOLS ||
token.kind == MusicScoreLexerConstants.UNKNOWN) {
System.out.println("syntax error");
} else {
System.out.println("TOKEN: " + tokenImage(token.kind) + ", Value: " +
token.image);
}
}
}
TOKEN: {
<CLEF: "Clef">
| <KEY_SIGNATURE: "KeySignature">
| <TIME_SIGNATURE: "TimeSignature">
| <BAR_LINE: "Bar_Line">
| <NOTE: "Note">
| <REST: "Rest">
| <DYNAMICS: "Dynamics">
| <ID: (["a"-"z", "A"-"Z"])+>
| <NUMBER: (["0"-"9"])+>
}
void Tokens():
{}
{
(
<CLEF> | <KEY_SIGNATURE> | <TIME_SIGNATURE> | <BAR_LINE> | <NOTE> |
<REST> | <DYNAMICS>
| <NUMBER> | <ID>
)*
}
// Main.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
INPUT FILE:
OUTPUT: