parser challanging of Xola

This commit is contained in:
anon 2024-04-03 15:44:16 +02:00
parent fa1d1f5cfd
commit d81ed3da1c
4 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,64 @@
#include <xolatile/xtandard.c>
#include <xolatile/xyntax.c>
int main (int argc, char * * argv) {
int index = 0;
int offset = 0;
int length = 0;
int select = 0;
syntax_define (false, true, "Head", "HCST.", '\\', colour_red, effect_bold);
syntax_define (false, true, "Circle", "HCST.", '\\', colour_green, effect_bold);
syntax_define (false, true, "Square", "HCST.", '\\', colour_yellow, effect_bold);
syntax_define (false, true, "Tail", "HCST.", '\\', colour_blue, effect_bold);
for (index = 1; index < argc; ++index) {
char * input = argv [index];
int count = 0;
int token [24] = { 0 };
for (offset = 0; input [offset] != '\0'; offset += length) {
select = syntax_select (& input [offset], & length);
if (select >= syntax_count) {
terminal_colour (colour_white, effect_normal);
} else {
terminal_colour (syntax_colour [select], syntax_effect [select]);
}
out (& input [offset], length);
terminal_cancel ();
if (string_compare_limit (& input [offset], "Head", 4) == true) token [count] = 1;
if (string_compare_limit (& input [offset], "Circle", 6) == true) token [count] = 2;
if (string_compare_limit (& input [offset], "Square", 6) == true) token [count] = 3;
if (string_compare_limit (& input [offset], "Tail", 4) == true) token [count] = 4;
++count;
}
echo (" -> ");
for (offset = 0; offset < count - 1; ++offset) {
if ((token [offset] == 1) && (token [offset + 1] == 2) && (token [offset + 2] == 4)) {
echo ("1");
offset += 2;
} else if ((token [offset] == 1) && (token [offset + 1] == 3) && (token [offset + 2] == 4) && (token [offset + 3] == 3)) {
echo ("3");
offset += 3;
} else if ((token [offset] == 1) && (token [offset + 1] == 3) && (token [offset + 2] == 4)) {
echo ("2");
offset += 2;
} else {
echo ("Invalid tokens...\n");
return (log_failure);
}
}
echo ("\n");
}
return (log_success);
}

View File

@ -0,0 +1,19 @@
%{
#include "main.tab.h"
const char input_str[] = "HeadCircleTailHeadSquareTailHeadSquareTailSquare";
const int len = sizeof(input_str)-1;
int offset = len;
#define YY_INPUT(buf, result, max_size) { \
int cpi = (offset && offset > max_size) ? max_size : offset; \
memcpy(buf, input_str+(len-offset), cpi); \
result = cpi; \
offset = (cpi > offset) ? 0 : offset - cpi; \
}
%}
%option noyywrap
%%
Head { return HEAD; }
Square { return SQUARE; }
Circle { return CIRCLE; }
Tail { return TAIL; }
%%

View File

@ -0,0 +1,39 @@
/*
@BAKE
bison --header=main.tab.h $@ -Wcounterexamples
flex l.l
gcc main.tab.c lex.yy.c -ggdb
./a.out
@STOP
*/
%{
#include <stdio.h>
void yyerror() { ; }
%}
%token HEAD CIRCLE SQUARE TAIL
%%
document: %empty
| document number
;
number: one
| two
| three
;
one: HEAD CIRCLE TAIL { fputs("1", stdout); }
;
two: HEAD SQUARE TAIL { fputs("2", stdout); }
;
three: HEAD SQUARE TAIL SQUARE { fputs("3", stdout); }
%%
signed main() {
yyparse();
puts("");
return 0;
}

View File

@ -0,0 +1,13 @@
Write a parser that accepts an arbitrary long string.
The valid keywords are:
Head
Circle
Square
Tail
The following input sequences should yield the following outputs:
HeadCircleTail -> 1
HeadSquareTail -> 2
HeadSquareTailSquare -> 3
Multiple inputs can be chained together in the input. E.g.:
HeadCircleTailHeadSquareTailSquare -> 13
Invalid input shall be rejected.