Skip to content

Commit

Permalink
Începe să lucrezi la tema 2 la TC
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielMajeri committed Mar 26, 2021
1 parent 0c072fe commit 24d8997
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tc/README.md
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
1 change: 1 addition & 0 deletions tc/tema2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
5 changes: 5 additions & 0 deletions tc/tema2/HelloWorld.java
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!");
}
}
35 changes: 35 additions & 0 deletions tc/tema2/Makefile
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 $@ $<
5 changes: 5 additions & 0 deletions tc/tema2/common.hpp
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()
29 changes: 29 additions & 0 deletions tc/tema2/java-lexer.ypp
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();

%%
60 changes: 60 additions & 0 deletions tc/tema2/java-parser.ypp
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();
}

0 comments on commit 24d8997

Please sign in to comment.