0% found this document useful (0 votes)
23 views7 pages

Led 3

Uploaded by

S. IMRAN BASHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Led 3

Uploaded by

S. IMRAN BASHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

%{

#include <stdio.h>

#include <math.h>

#include <string.h>

int yylex (void);

void yyerror (char const *);

extern char val[100];

%}

%define api.value.type {char *}

%token DEF 1

%token INTEGER 2

%token REAL 3

%token STRING 4

%token IF 5

%token THEN 6

%token WHILE 7

%token REAL_CONST 8

%token ID 9

%token INT_CONST 10

%token STRING_CONST 11

%token SEMI 12

%token COLON 13

%token LEFT_PAREN 14

%token RIGHT_PAREN 15

%token PLUS 16

%token MINUS 17

%token MULT 18

%token DIVIDE 19

%token CARAT 20

%token MOD 21
%token EQUAL 22

%token NOT_EQUAL 23

%token LESS_THAN 24

%token GREATER_THAN 25

%token LESS_EQUAL 26

%token GREATER_EQUAL 27

%token ASSIGN 28

%token LEFT_SQUARE 29

%token RIGHT_SQUARE 30

%token LEFT_BRACE 31

%token RIGHT_BRACE 32

%token COMMA 33

%% /* Grammar rules and actions follow. */

input : statement_list

statement_list

| statement statement_list

statement

: variable_definition

| assignment_statement

| conditional_statement

| loop_statement

| block_statement

| expression SEMI

variable_definition

: DEF identifier COLON type SEMI


{ printf("Variable %s defined as a %s\n", $2, $4); }

identifier

: ID

{ $$ = strdup(val); }

type : INTEGER

{ $$ = "integer"; }

| REAL

{ $$ = "real"; }

| STRING

{ $$ = "string"; }

assignment_statement

: identifier ASSIGN expression SEMI

{ printf("Assigning %s to an expression\n", $1); }

constant

: REAL_CONST

{ $$ = strdup(val); }

| INT_CONST

{ $$ = strdup(val); }

| STRING_CONST

{ $$ = strdup(val); }

operator

: PLUS

{ $$ = "addition"; }

given: emcee.l
%option noyywrap

D [0-9]

A [a-zA-Z]

%{

#include "tokens.h"

char val[100];

%}

%%

def return (DEF);

integer return (INTEGER);

real return (REAL);

string return (STRING);

if return (IF);

then return (THEN);

while return (WHILE);

{D}+"."{D}+ { sprintf(val, "Real:%s", yytext); return (REAL_CONST); }

{A}+ { strcpy(val, yytext); return (ID); }

{D}+ { sprintf(val, "Integer:%s", yytext); return (INT_CONST); }

\"[^"]*\" { sprintf(val, "String:%s", yytext); return (STRING_CONST); }

";" return (SEMI);

":" return (COLON);

"(" return (LEFT_PAREN);

")" return (RIGHT_PAREN);

"+" return (PLUS);

"-" return (MINUS);

"*" return (MULT);

"/" return (DIVIDE);

"^" return (CARAT);

"%" return (MOD);


"=" return (EQUAL);

"!=" return (NOT_EQUAL);

"<" return (LESS_THAN);

">" return (GREATER_THAN);

"<=" return (LESS_EQUAL);

">=" return (GREATER_EQUAL);

":=" return (ASSIGN);

"[" return (LEFT_SQUARE);

"]" return (RIGHT_SQUARE);

"{" return (LEFT_BRACE);

"}" return (RIGHT_BRACE);

"," return (COMMA);

^#.* ; /* Ignore comments */

. ; /* ignore any unmatched chars */

"\n" ;

%%

void yyerror () /* default action in case of error in yylex() */

printf (" error\n");

exit(0);

given: emceeparse.c

#include <stdio.h>

int yyparse();

main () {

printf("Emcee Compiler � ver. 0.2\n\n");

yyparse();

given: tokens.h

#define DEF 1
#define INTEGER 2

#define REAL 3

#define STRING 4

#define IF 5

#define THEN 6

#define WHILE 7

#define REAL_CONST 8

#define ID 9

#define INT_CONST 10

#define STRING_CONST 11

#define SEMI 12

#define COLON 13

#define LEFT_PAREN 14

#define RIGHT_PAREN 15

#define PLUS 16

#define MINUS 17

#define MULT 18

#define DIVIDE 19

#define CARAT 20

#define MOD 21

#define EQUAL 22

#define NOT_EQUAL 23

#define LESS_THAN 24

#define GREATER_THAN 25

#define LESS_EQUAL 26

#define GREATER_EQUAL 27

#define ASSIGN 28

#define LEFT_SQUARE 29

#define RIGHT_SQUARE 30
#define LEFT_BRACE 31

#define RIGHT_BRACE 32

#define COMMA 33

given: sqrt.mc

# First program written in Emcee

def guess : real;


def n : real;

# Set the input variables


guess := 1.0;
n := inputReal();

if [ n >= 0.0 ] then


{

# Continue until the root has been found

while [ abs(guess^2 - n) > 0.01 ]


{
guess := (n + (n / guess)) / 2;
};

# Print the result


print("Root is ", guess);
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy