-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c072fe
commit 24d8997
Showing
7 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Tehnici de Compilare | ||
|
||
- [Tema 1](tema1): Lexer pentru limbajul Haskell folosind GNU flex | ||
- [Tema 2](tema2): Parser pentru limbajul Java folsoind GNU bison |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class HelloWorld { | ||
public static void main(String[] args) { | ||
System.out.println("Hello, world!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
CXXFLAGS := -Wall -Wextra | ||
LDFLAGS := | ||
BISONFLAGS := | ||
|
||
.PHONY: compile clean | ||
|
||
run: build/java-parser | ||
./$< HelloWorld.java | ||
|
||
compile: | ||
build/java-parser | ||
|
||
clean: | ||
-rm -r build | ||
|
||
build: | ||
mkdir -p build | ||
|
||
build/common.hpp: common.hpp | ||
cp $< $@ | ||
|
||
build/java-parser: build/java-parser.o build/java-lexer.o | build | ||
c++ $(LDFLAGS) -o $@ $^ | ||
|
||
build/java-parser.o: build/java-parser.cpp build/common.hpp | build | ||
c++ $(CXXFLAGS) -c -o $@ $< | ||
|
||
build/java-parser.cpp: java-parser.ypp | build | ||
bison $(BISONFLAGS) -d -o $@ $< | ||
|
||
build/java-lexer.o: build/java-lexer.cpp build/common.hpp | build | ||
cc $(CFLAGS) -c -o $@ $< | ||
|
||
build/java-lexer.cpp: java-lexer.ypp | build | ||
flex -o $@ $< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#pragma once | ||
|
||
#include "java-parser.hpp" | ||
|
||
#define YY_DECL yy::parser::symbol_type yylex() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** Lexical analyser for Java */ | ||
|
||
/* Disable unneeded features */ | ||
%option noyywrap nounput noinput batch | ||
|
||
/* Enable debug mode */ | ||
%option debug | ||
|
||
%{ | ||
// Include the common interface | ||
#include "common.hpp" | ||
|
||
// Include the parser's function definitions | ||
#include "java-parser.hpp" | ||
%} | ||
|
||
BLANK [[:space:]] | ||
|
||
DIGIT [0-9] | ||
|
||
INTEGER {DIGIT}+ | ||
|
||
%% | ||
|
||
{INTEGER} return yy::parser::make_NUMBER(std::stoi(yytext)); | ||
|
||
<<EOF>> return yy::parser::make_YYEOF(); | ||
|
||
%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Require a modern version of Bison | ||
%require "3.5" | ||
|
||
// Generate C++ code | ||
%language "c++" | ||
|
||
// Use a type-save variant data structure for storing values | ||
%define api.value.type variant | ||
|
||
// Generate the various `make_<TOKEN>` functions | ||
%define api.token.constructor | ||
|
||
%{ | ||
#include "common.hpp" | ||
YY_DECL; | ||
%} | ||
|
||
%% | ||
|
||
result: | ||
list { | ||
std::cout << "List is: "; | ||
for (const auto& s : $1) { | ||
std::cout << s << ' '; | ||
} | ||
std::cout << '\n'; | ||
} | ||
; | ||
|
||
%nterm <std::vector<std::string>> list; | ||
list: | ||
%empty { /* Generates an empty string list */ } | ||
| list item { $$ = $1; $$.push_back ($2); } | ||
; | ||
|
||
%nterm <std::string> item; | ||
%token <std::string> TEXT; | ||
%token <int> NUMBER; | ||
|
||
item: | ||
TEXT | ||
| NUMBER { $$ = std::to_string ($1); } | ||
; | ||
|
||
%% | ||
|
||
namespace yy | ||
{ | ||
// Report an error to the user. | ||
void parser::error(const std::string& message) | ||
{ | ||
std::cerr << message << '\n'; | ||
} | ||
} | ||
|
||
int main () | ||
{ | ||
yy::parser java_parser; | ||
return java_parser(); | ||
} |