TIDE
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 flex

Verification

Check that Yacc is installed and accessible:

yacc --version

Writing 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.y

Compile the generated y.tab.c with GCC:

gcc y.tab.c -o parser

Run the parser executable:

./parser

Official Resources

On this page