Languages
Yacc
Installing Yacc on Ubuntu in TIDE
Yacc
Yacc (Yet Another Compiler-Compiler) is a classic LALR parser generator used to build compilers, interpreters, and domain-specific language parsers. On Ubuntu, Berkeley Yacc (byacc) and GNU Bison provide standard Yacc functionality.
Installation
Install Berkeley Yacc, GNU Bison, GCC, and Flex using apt:
apt update
apt install -y byacc bison build-essential flexVerification
Check that Yacc is installed and accessible:
yacc --versionWriting and Compiling Code
Create a Yacc grammar file named parser.y:
%{
#include <stdio.h>
int yylex(void);
void yyerror(const char *s);
%}
%%
program:
/* empty */
| program statement
;
statement:
/* empty */
;
%%
int yylex(void) {
static int done = 0;
if (done) return 0;
done = 1;
printf("Hello, World!\n");
return 0;
}
void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}
int main(void) {
yyparse();
return 0;
}Generate the C parser source file with Yacc:
yacc -d parser.yCompile the generated y.tab.c with GCC:
gcc y.tab.c -o parserRun the parser executable:
./parser