You are on page 1of 6

Activity based

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

Department of Computer Engineering


Faculty of Science and Technology

Academic Year
2023-2024
Compiler Design BTECCE21603

TOPIC: Compiler Design for Music Score language in Java


language

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.

i. Role of Lexical Analyzer in Compilation


The lexical analyzer plays a crucial role in the compilation process. Its primary
work is to read the input source code character by character and group them into
tokens. These tokens are then passed on to the parser for further processing.
The lexical analyzer's role in compiling a music score language would be to:

• Tokenize the input music score text.


• Recognize tokens based on the rules of the music score language.
• Output tokens with their types.
• Detect and report lexical errors.
• Pass the tokens to the parser for further analysis of the music score's
structure and semantics.

ii. Tokens for the language specification in the problem


A music score language, also known as a music notation language, is a system of
symbols and rules used to represent musical notes, rhythms, dynamics,
articulations, and other musical elements on paper or digitally.
One of the most common and widely used music score languages is Standard
Music Notation. Tokens will comprise of the Key components of Standard Music
Notation.
TOKENS:
1. Notes: Represented by pitch names (A, B, C, etc.), durations (quarter note,
half note, etc.)
2. Rests: Symbols that denote periods of silence.
3. Clefs: Symbols placed at the beginning of a staff to indicate the pitch range.
4. Key Signatures: Indicates the key of the piece and the notes that are altered
throughout.
5. Time Signatures: Indicate the meter or rhythm of the piece.
6. Bar Lines: Symbols indicating the end of a measure.
7. Dynamics: Symbols indicating the volume or intensity of the music.
iii. Implementations of Lexical Analyzer

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.

The implementation of a Lexical Analyzer for a music score language would be as


follows:

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:

• Regular expressions might be used to match pitch names (A-G), durations


(e.g., 1, 2, 4 for whole, half, quarter notes), and symbols like rests and bar
lines.
• Specific rules might be defined for recognizing time signatures (e.g., 4/4,
3/4), key signatures (e.g., number of sharps or flats), and dynamics (e.g., p,
mf, ff).

3. Token Output:

As the lexical analyzer identifies tokens, it would output them with their
corresponding types. For example:

TOKEN: NOTE, Value: C4, Duration: Quarter Note


TOKEN: REST, Duration: Half Note
TOKEN: BAR_LINE
TOKEN: TIME_SIGNATURE, Value: 4/4

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);
}
}
}

static String tokenImage(int kind) {


switch (kind) {
case MusicScoreLexerConstants.CLEF:
return "CLEF";
case MusicScoreLexerConstants.KEY_SIGNATURE:
return "KEY_SIGNATURE";
case MusicScoreLexerConstants.TIME_SIGNATURE:
return "TIME_SIGNATURE";
case MusicScoreLexerConstants.BAR_LINE:
return "BAR_LINE";
case MusicScoreLexerConstants.NOTE:
return "NOTE";
case MusicScoreLexerConstants.REST:
return "REST";
case MusicScoreLexerConstants.DYNAMICS:
return "DYNAMICS";
default:
return "UNKNOWN";
}
}
}
PARSER_END(MusicScoreLexer)

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 file Code:

// Main.java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {


public static void main(String[] args) {
try {
InputStream inputStream = new FileInputStream("input_music_score.txt");
MusicScoreLexer lexer = new MusicScoreLexer(inputStream);
Token token;
while ((token = lexer.getNextToken()).kind !=
MusicScoreLexerConstants.EOF) {
System.out.println("TOKEN: " + token.kind + ", Value: " + token.image);
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

INPUT FILE:

OUTPUT:

You might also like