Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdlib.h>
- #include <stdio.h>
- #define NUMB 256
- #define CONS 257
- #define APPEND 258
- #define DROP 259
- #define TAKE 260
- int yylval;
- %}
- %%
- [+,()\[\]\n] { return *yytext; }
- [0-9]+ { yylval = atoi(yytext);
- return NUMB; }
- "take" { return TAKE; }
- "@" { return APPEND; }
- "drop" { return DROP; }
- "::" { return CONS; }
- %%
- ==============================================
- #include <stdlib.h>
- #include <stdio.h>
- #define NUMB 256
- #define CONS 257
- #define APPEND 258
- #define DROP 259
- #define TAKE 260
- extern int yylval;
- int symbol;
- typedef struct _cons{
- int car;
- struct _cons* cdr;
- }cons;
- void print_list(cons *);
- cons *concat(cons *, cons *);
- cons *consf(int, cons *);
- cons *take(cons *, int);
- cons *drop(cons*, int);
- cons *cdr(cons *);
- int car (cons *);
- cons* L();
- cons* E();
- cons *Enum();
- void next_symbol() {
- symbol = yylex();
- }
- void error(int nr) {
- printf("Syntax error: %d\n", nr);
- }
- void parser()
- {
- print_list(E());
- //next_symbol();
- if (symbol != '\n')
- error(0);
- }
- cons *E() {
- cons *firstL = L();
- next_symbol();
- if (symbol == APPEND) {
- next_symbol();
- cons *rez = E();
- return concat(firstL, rez);
- } else {
- return firstL;
- }
- }
- cons *L() {
- //cons *res = (cons *)malloc(sizeof(cons));
- if (symbol == '[') {
- next_symbol();
- cons *res = Enum();
- if (symbol == ']') {
- return res;
- }
- }
- else {
- if (symbol == TAKE) {
- next_symbol(); // read '('
- if (symbol == '(') {
- next_symbol(); // read nr
- int nr = yylval;
- next_symbol(); // read ','
- next_symbol(); // read '['
- cons *res = E();
- if (symbol == ')') {
- return take(res, nr);
- } else {
- printf("%d", symbol);
- error(12);
- }
- } else {
- error(3);
- return NULL;
- }
- } else {
- if (symbol == DROP) {
- next_symbol(); // read '('
- if (symbol == '(') {
- next_symbol(); // read nr
- int nr = yylval;
- next_symbol(); // read ','
- next_symbol(); // read '['
- cons *res = E();
- if (symbol == ')') {
- return drop(res, nr);
- }
- }
- }
- else {
- if (symbol == NUMB) {
- int currentNr = yylval;
- next_symbol();
- if(symbol == CONS) {
- next_symbol();
- return consf(currentNr, L());
- }
- } else {
- error(33);
- return NULL;
- }
- }
- }
- }
- }
- cons *Enum() {
- if (symbol == NUMB) {
- int nr = yylval;
- next_symbol();
- if (symbol == ',') {
- next_symbol();
- cons *l = Enum();
- return consf(nr, l);
- } else{ // found ']'
- //next_symbol();
- return consf(nr, NULL);
- }
- }
- }
- int car (cons *l) {
- return l->car;
- }
- cons *cdr(cons *l) {
- if (l == NULL)
- return NULL;
- return l->cdr;
- }
- cons *take(cons *l, int nr) {
- cons *rez = (cons *)malloc(sizeof(cons));
- rez->car = l->car;
- l = l->cdr;
- nr--;
- for (int i = 0; i < nr; i++) {
- concat(rez, consf(l->car, NULL));
- l = l->cdr;
- }
- return rez;
- }
- cons *drop(cons *l, int nr) {
- cons *rez = (cons *)malloc(sizeof(cons));
- rez->car = l->car;
- l = l->cdr;
- nr--;
- for (int i = 0; i < nr; i++) {
- concat(rez, consf(l->car, NULL));
- l = l->cdr;
- }
- return l;
- }
- cons *consf(int el, cons *l) {
- cons *rez = (cons *)malloc(sizeof(cons));
- rez->car = el;
- rez->cdr = l;
- return rez;
- }
- cons *concat(cons *l1, cons *l2) {
- cons *it = l1;
- if (l1 == NULL) {
- return l2;
- }
- while(it->cdr != NULL) {
- it = it->cdr;
- }
- it->cdr = l2;
- return l1;
- }
- void print_list(cons *l) {
- printf("[");
- if(l == NULL) {
- printf("]\n");
- return;
- }
- while(l->cdr != NULL) {
- printf("%d ", l->car);
- l = l->cdr;
- }
- printf("%d]\n", l->car);
- }
- void main() {
- next_symbol();
- while (symbol != 0) {
- parser();
- next_symbol();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement