NO

Author Topic: CLR Parser yaclrcc  (Read 2193 times)

Offline cosh

  • Member
  • *
  • Posts: 33
CLR Parser yaclrcc
« on: February 11, 2024, 04:23:19 PM »
Hi, everyone.
I finished writing a canonical LR(1) parser. https://github.com/coshcage/yaclrcc
I hope this parser could be useful to you guys who want a pure C style parsing program.
The technique behind this parser is in the book Compilers Principles, Techniques and Tools (Second Edition).
However you can also advance this parser by involve developing it.
I tested this parser by using Pelles C and MSVC.
This is the file that I used to launch the parser:
Code: [Select]
#include <stdio.h>
#include <wchar.h>
#include "yaclrcc.h"

#define STR L"A : S;\nS : C C;\nC : 1 C;\nC : 2;\n"

size_t i, j;

const wchar_t * wc = L"ccccdddd";

P_QUEUE_L pq;

ptrdiff_t GetSymbol(void)
{
if (i < j)
{
return Lexer(pq, wc[i++]);
}
return 0;
}

int Reduce(ptrdiff_t n)
{
printf("REDUCE! %ld\n", n);
return CBF_TERMINATE;
}

void Error(void)
{
printf("ERROR!\n");
return CBF_TERMINATE;
}

int main()
{

P_MATRIX ptbl;
P_ARRAY_Z parrG = NULL;
pq = LexCompile(L"c\nd\n");

ptbl = ConstructCLRTable(STR, &parrG);

PrintCLRTable(ptbl);

j = wcslen(wc);

CLRParse(ptbl, parrG, GetSymbol, Reduce, Error);

LexDestroy(pq);

DestroyParrList(parrG);

DestroyCLRTable(ptbl);

return 0;
}
The parser reads this:
A : S;
S : C C;
C : 1 C;
C : 2;
grammar in and parse it to generate a parsing table like:
(-3)    (-2)    (1)     (2)     (2147483647)
 3       2       4       5       0
 0       0       0       0       2147483647
 6       0       7       8       0
 9       0       4       5       0
 0       0      r3      r3       0
 0       0       0       0      r1
 10      0       7       8       0
 0       0       0       0      r3
 0       0      r2      r2       0
 0       0       0       0      r2
REDUCE! 3
In the above grammar A,B and C instead of none-terminator and 1,2 means terminator from the lexical analyzer.
In the table, 2147483647 means ACC status.
I hope you enjoy this parser.
Regards.

Offline John Z

  • Member
  • *
  • Posts: 840
Re: CLR Parser yaclrcc
« Reply #1 on: February 13, 2024, 11:09:01 PM »
Hi cosh,

Congratulations!  This is so far out of my wheelhouse it's like reading Greek to me!  But I'm sure others cognizant in this area will appreciate your efforts!

John Z

Offline cosh

  • Member
  • *
  • Posts: 33
Re: CLR Parser yaclrcc
« Reply #2 on: February 17, 2024, 02:26:29 PM »
Hi cosh,

Congratulations!  This is so far out of my wheelhouse it's like reading Greek to me!  But I'm sure others cognizant in this area will appreciate your efforts!

John Z

Thank you for your encouragement. Actually, Parser is a gadget that a compiler uses. We say a compiler contains these procedures: Lexical analyzing, Syntax analyzing(Parsing), Intermediate code generation and target code generation. You may reference to books that titled with compilers. :)