unix_prog_environ / hoc.y

%{
#define	YYSTYPE double

#include <stdio.h>
#include <ctype.h>
#include "hoc.h"
%}
%token	NUMBER
%left	'+' '-'
%left	'*' '/'
%%
list:
		| list '\n'
		| list expr '\n' { printf("%.8g\n", $2); }
		;
expr:	  NUMBER		{ $$ = $1; }
		| expr '+' expr { $$ = $1 + $3; }
		| expr '-' expr { $$ = $1 - $3; }
		| expr '*' expr { $$ = $1 * $3; }
		| expr '/' expr { $$ = $1 / $3; }
		| '(' expr ')'  { $$ = $2; }
		;
%%
char *progname;
int  lineno = 1;

int
main(int argc, char **argv)
{
	progname = argv[0];
	yyparse();
	return 0;
}

int
yylex()
{
	int c;
	while((c = getchar()) == ' ' || c == '\t');
	if(c == EOF) {
		return 0;
	}
	if(c == '.' || isdigit(c)) {
		ungetc(c, stdin);
		scanf("%lf", &yylval);
		return NUMBER;
	}
	if(c == '\n') {
		lineno++;
	}
	return c;
}

void
yyerror(char *s, ...)
{
	warning(s, (char *)0);
}

void
warning(char *s, char *t)
{
	fprintf(stderr, "%s: %s", progname, s);
	if(t) {
		fprintf(stderr, "%s", t);
	}
	fprintf(stderr, " near line %d\n", lineno);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.