|
Packit |
f00812 |
/*
|
|
Packit |
f00812 |
* This file is part of flex.
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
f00812 |
* modification, are permitted provided that the following conditions
|
|
Packit |
f00812 |
* are met:
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
f00812 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
f00812 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
f00812 |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
f00812 |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* Neither the name of the University nor the names of its contributors
|
|
Packit |
f00812 |
* may be used to endorse or promote products derived from this software
|
|
Packit |
f00812 |
* without specific prior written permission.
|
|
Packit |
f00812 |
*
|
|
Packit |
f00812 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
Packit |
f00812 |
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
Packit |
f00812 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
Packit |
f00812 |
* PURPOSE.
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/*
|
|
Packit |
f00812 |
How to compile:
|
|
Packit |
f00812 |
bison --defines --output-file="parser.c" --name-prefix="test" parser.y
|
|
Packit |
f00812 |
*/
|
|
Packit |
f00812 |
%{
|
|
Packit |
f00812 |
#include <stdio.h>
|
|
Packit |
f00812 |
#include <stdlib.h>
|
|
Packit |
f00812 |
#include <string.h>
|
|
Packit |
f00812 |
#include "config.h"
|
|
Packit |
f00812 |
#include "bison_nr_parser.h"
|
|
Packit |
f00812 |
#include "bison_nr_scanner.h"
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
#define YYERROR_VERBOSE 1
|
|
Packit |
f00812 |
/* #define YYPARSE_PARAM scanner */
|
|
Packit |
f00812 |
/* #define YYLEX_PARAM scanner */
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
int yyerror(const char* msg);
|
|
Packit |
f00812 |
extern int testget_lineno(void);
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* A dummy function. A check against seg-faults in yylval->str. */
|
|
Packit |
f00812 |
int process_text(char* s) {
|
|
Packit |
f00812 |
int total =0;
|
|
Packit |
f00812 |
while(*s) {
|
|
Packit |
f00812 |
total += (int) *s;
|
|
Packit |
f00812 |
++s;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
return total;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
%}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
%pure-parser
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
%union {
|
|
Packit |
f00812 |
int lineno;
|
|
Packit |
f00812 |
char * str;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
%token <str> IDENT
|
|
Packit |
f00812 |
%token <lineno> LINENO
|
|
Packit |
f00812 |
%token EQUAL "="
|
|
Packit |
f00812 |
%token COLON ":"
|
|
Packit |
f00812 |
%token SPACE " "
|
|
Packit |
f00812 |
%%
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
file:
|
|
Packit |
f00812 |
line
|
|
Packit |
f00812 |
| file line
|
|
Packit |
f00812 |
;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
line:
|
|
Packit |
f00812 |
LINENO COLON SPACE IDENT EQUAL IDENT
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
process_text($4);
|
|
Packit |
f00812 |
process_text($6);
|
|
Packit |
f00812 |
/* Check lineno. */
|
|
Packit |
f00812 |
if( $1 != @1.first_line || $1 != testget_lineno())
|
|
Packit |
f00812 |
{
|
|
Packit |
f00812 |
yyerror("Parse failed: Line numbers do not match.");
|
|
Packit |
f00812 |
YYABORT;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
/* Recreate the line to stdout. */
|
|
Packit |
f00812 |
printf ( "%04d: %s=%s\n", @1.first_line, $4, $6);
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
;
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
%%
|
|
Packit |
f00812 |
|
|
Packit |
f00812 |
int yyerror(const char* msg) {
|
|
Packit |
f00812 |
fprintf(stderr,"%s\n",msg);
|
|
Packit |
f00812 |
return 0;
|
|
Packit |
f00812 |
}
|
|
Packit |
f00812 |
|