Skip to content

Commit

Permalink
Termină tema la TC
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielMajeri committed Apr 4, 2021
1 parent 24d8997 commit b7ba40a
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 26 deletions.
19 changes: 19 additions & 0 deletions tc/tema2/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// This is a class.
class HelloWorld {
private final static int MY_CONSTANT = 1234;

public static void main(String[] args) {
System.out.println("Hello, world!");

String myString = "Test";
int x = 5;
System.out.println(myString);

System.out.println(HelperClass.compute(convert(x)));
}

// This is a private static method
private static String convert(int input) {
return input.toString();
}

/** This is an instance method. */
public int getValue() {
return 15;
}
}
22 changes: 20 additions & 2 deletions tc/tema2/common.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
#pragma once

#include "java-parser.hpp"

#define YY_DECL yy::parser::symbol_type yylex()

enum class AccessModifier
{
DEFAULT,
PUBLIC,
PROTECTED,
PRIVATE
};

enum class MutabilityModifier
{
MUTABLE,
FINAL
};

enum class StorageModifier
{
INSTANCE,
STATIC
};
41 changes: 40 additions & 1 deletion tc/tema2/java-lexer.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%option noyywrap nounput noinput batch

/* Enable debug mode */
%option debug
/* %option debug */

%{
// Include the common interface
Expand All @@ -20,10 +20,49 @@ DIGIT [0-9]

INTEGER {DIGIT}+

IDENTIFIER [[:alpha:]]([[:alnum:]]|_)*

/* Define an exclusive mode for the multi line comment */
%x COMMENT

%%

"//".+ /* Ignore single line comment */

<INITIAL>"/*" BEGIN(COMMENT);
<COMMENT>"*/" BEGIN(INITIAL);
<COMMENT>.|"\n" /* Ignore comment contents */

class return yy::parser::make_CLASS_KEYWORD();

public return yy::parser::make_PUBLIC_KEYWORD();
protected return yy::parser::make_PROTECTED_KEYWORD();
private return yy::parser::make_PRIVATE_KEYWORD();

final return yy::parser::make_FINAL_KEYWORD();
static return yy::parser::make_STATIC_KEYWORD();

return return yy::parser::make_RETURN_KEYWORD();

"(" return yy::parser::make_OPEN_PARENS();
")" return yy::parser::make_CLOSE_PARENS();
"[" return yy::parser::make_OPEN_SQUARE_BRACKET();
"]" return yy::parser::make_CLOSE_SQUARE_BRACKET();
"{" return yy::parser::make_OPEN_BRACKET();
"}" return yy::parser::make_CLOSE_BRACKET();
"=" return yy::parser::make_EQUALS();
"," return yy::parser::make_COMMA();
";" return yy::parser::make_SEMICOLON();
"." return yy::parser::make_DOT();

{INTEGER} return yy::parser::make_NUMBER(std::stoi(yytext));

\".+\" return yy::parser::make_STRING(yytext);

{IDENTIFIER} return yy::parser::make_IDENTIFIER(yytext);

[[:space:]] /* Ignore whitespace */

<<EOF>> return yy::parser::make_YYEOF();

%%
200 changes: 177 additions & 23 deletions tc/tema2/java-parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,186 @@
// Generate the various `make_<TOKEN>` functions
%define api.token.constructor

%{
// Include the common interfaces in the generated header
%code requires {
#include "common.hpp"
}

// Declare the `yylex` method, which will be defined in the lexer
%code {
YY_DECL;
%}
}

%%

result:
list {
std::cout << "List is: ";
for (const auto& s : $1) {
std::cout << s << ' ';
}
std::cout << '\n';
}
;
%token CLASS_KEYWORD;

%token PUBLIC_KEYWORD;
%token PROTECTED_KEYWORD;
%token PRIVATE_KEYWORD;

%token FINAL_KEYWORD;
%token STATIC_KEYWORD;

%token RETURN_KEYWORD;

%nterm <std::vector<std::string>> list;
list:
%empty { /* Generates an empty string list */ }
| list item { $$ = $1; $$.push_back ($2); }
;
%token OPEN_PARENS;
%token CLOSE_PARENS;
%token OPEN_BRACKET;
%token CLOSE_BRACKET;
%token OPEN_SQUARE_BRACKET;
%token CLOSE_SQUARE_BRACKET;
%token EQUALS;
%token COMMA;
%token SEMICOLON;
%token DOT;

%nterm <std::string> item;
%token <std::string> TEXT;
%token <std::string> IDENTIFIER;
%token <int> NUMBER;
%token <std::string> STRING;

class_definition:
access_modifier CLASS_KEYWORD IDENTIFIER
OPEN_BRACKET class_body CLOSE_BRACKET
{
std::cout << "Defining class " << $3 << '\n';
}

%nterm class_body;
class_body:
definitions

definitions:
%empty | definition definitions

definition:
class_variable_definition | method_definition

class_variable_definition:
access_modifier mutability_modifier storage_modifier type_identifier name optional_initializer SEMICOLON
{
std::cout << "Defining variable " << $5 << '\n';
}

method_definition:
access_modifier mutability_modifier storage_modifier type_identifier name
OPEN_PARENS method_arguments CLOSE_PARENS
OPEN_BRACKET method_body CLOSE_BRACKET
{
std::cout << "Defining method " << $5 << '\n';
}

method_arguments:
%empty
| argument
| argument COMMA method_arguments

argument:
type_identifier IDENTIFIER

method_body:
statements

statements:
%empty | statement statements

statement:
variable_declaration
| assignment
| method_call SEMICOLON
| return_statement

variable_declaration:
type_identifier name optional_initializer SEMICOLON
{
std::cout << "Defining variable " << $2 << '\n';
}

assignment:
name initializer SEMICOLON

method_call:
method_reference
OPEN_PARENS actual_parameters CLOSE_PARENS
{
std::cout << "Calling method " << $1 << '\n';
}

return_statement:
RETURN_KEYWORD SEMICOLON
| RETURN_KEYWORD expression SEMICOLON

actual_parameters:
%empty
| value
| value COMMA actual_parameters

value:
name | expression

expression:
literal
| method_call

%nterm <std::string> method_reference;
method_reference:
name
{ $$ = $1; }
| name DOT method_reference
{ $$ = $1 + "." + $3; }

%nterm <AccessModifier> access_modifier;
access_modifier:
%empty
{ $$ = AccessModifier::DEFAULT; }
| PUBLIC_KEYWORD
{ $$ = AccessModifier::PUBLIC; }
| PROTECTED_KEYWORD
{ $$ = AccessModifier::PROTECTED; }
| PRIVATE_KEYWORD
{ $$ = AccessModifier::PRIVATE; }

%nterm <MutabilityModifier> mutability_modifier;
mutability_modifier:
%empty
{ $$ = MutabilityModifier::MUTABLE; }
| FINAL_KEYWORD
{ $$ = MutabilityModifier::FINAL; }

%nterm <StorageModifier> storage_modifier;
storage_modifier:
%empty
{ $$ = StorageModifier::INSTANCE; }
| STATIC_KEYWORD
{ $$ = StorageModifier::STATIC; }

item:
TEXT
| NUMBER { $$ = std::to_string ($1); }
;
type_identifier:
IDENTIFIER
{
std::cout << "Referencing object type " << $1 << '\n';
}
| IDENTIFIER OPEN_SQUARE_BRACKET CLOSE_SQUARE_BRACKET
{
std::cout << "Referencing array type " << $1 << "[]" << '\n';
}

%nterm <std::string> name;
name:
IDENTIFIER

optional_initializer:
%empty | initializer

initializer:
EQUALS literal
{
std::cout << "Initializing to " << $2 << '\n';
}

%nterm <std::string> literal;
literal:
STRING | NUMBER
{ $$ = std::to_string($1); }

%%

Expand All @@ -53,8 +202,13 @@ namespace yy
}
}

int main ()
int main(int argc, char* argv[])
{
if (argc > 1)
{
freopen(argv[1], "r", stdin);
}

yy::parser java_parser;
return java_parser();
}

0 comments on commit b7ba40a

Please sign in to comment.