Skip to content

Instantly share code, notes, and snippets.

@maifeeulasad
Created August 31, 2020 07:45
Show Gist options
  • Save maifeeulasad/6d0ea58cd70fbe255a4834eb46f2e1fd to your computer and use it in GitHub Desktop.
Save maifeeulasad/6d0ea58cd70fbe255a4834eb46f2e1fd to your computer and use it in GitHub Desktop.

Just testing

Goal:

Let test.l do only lexing part Let test.y do only parsing part main.cpp has the main function, and all the definations are at main.hpp Ob and Val are just two objects.

Build rules are defined at build.bat

flex test.l
bison -d test.y
g++ lex.yy.c test.tab.c *.hpp -o test.exe
C:\Users\MUA\AppData\Local\Temp\ccs2u9Zf.o:test.tab.c:(.text+0x3a4): undefined reference to `Ob::AddValue(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:\Users\MUA\AppData\Local\Temp\ccs2u9Zf.o:test.tab.c:(.text+0x499): undefined reference to `yyerror(char const*)'
C:\Users\MUA\AppData\Local\Temp\ccs2u9Zf.o:test.tab.c:(.text+0x5c0): undefined reference to `yyerror(char const*)'
E:/software/codeblocks-17.12/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe: C:\Users\MUA\AppData\Local\Temp\ccs2u9Zf.o: bad reloc address 0x0 in section `.ctors'
E:/software/codeblocks-17.12/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
#include <bits/stdc++.h>
#include <stdio.h>
#include "main.hpp"
using namespace std;
void yyerror (char const *s) {
fprintf (stderr, "%s\n", s);
}
int main(int argc, char **argv)
{
char buffer[BUFSIZ];
while (1)
{
printf("****************\n");
char* input = fgets(buffer, sizeof buffer, stdin);
if (buffer == NULL) break;
set_input(input);
yyparse();
}
return 0;
}
#include "test.tab.h"
void yyerror (char const *s);
int yylex(void);
void set_input(const char* input);
extern int yyparse();
#include <bits/stdc++.h>
#include "Ob.hpp"
using namespace std;
class Val;
Object::Object()
{
}
static void Object::AddValue(Val __val)
{
_val.push_back(__val);
}
static void AddValue(string __string)
{
_val.push_back(Val(__val));
}
#ifndef O_H_
#define O_H_
#include <bits/stdc++.h>
using namespace std;
class Val;
class Ob{
public:
Ob();
static void AddValue(Val __val);
static void AddValue(string __string);
private:
static vector<Val> _val;
};
#endif /*O_H_*/
%{
#include "test.tab.h"
%}
%option noinput nounput nodefault yylineno
%option noyywrap
%%
[(] {yylval.s = yytext;return START_P;}
[)] {yylval.s = yytext;return END_P;}
"," {yylval.s = yytext;return COMMA;}
[[:digit:]]+ {yylval.i = atoi(yytext);return INTEGER;}
\"(\$\{.*\}|\\.|[^\"\\])*\" {yylval.s = yytext;return STRING;}
[[:space:]]+ ;
. {return *yytext;}
%%
static YY_BUFFER_STATE flex_buffer;
void set_input(const char* input) {
yy_delete_buffer(flex_buffer);
flex_buffer = yy_scan_string(input);
}
%{
#include<stdio.h>
#include "main.hpp"
#include "Ob.hpp"
%}
%union {
char *s;
int i;
}
%token <s> STRING
%token <i> INTEGER
%token START_P
%token END_P
%token COMMA
%%
test : START_P value_set END_P
value_set : value
| value_set COMMA value
value : INTEGER
| STRING {Ob::AddValue($1);}
%%
#include <bits/stdc++.h>
#include "Object.hpp"
using namespace std;
Val::Val(string __string)
{
_members[__string]=__string;
}
#ifndef V_H_
#define V_H_
#include <bits/stdc++.h>
using namespace std;
class Val{
public:
Val(string __string);
private:
map<string, string> _members;
};
#endif /*V_H_*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment