Discussion:
[ragel-users] Keywords and actions in a minimal example
Lane Schwartz
2013-06-25 18:20:00 UTC
Permalink
I have a minimal example, where tokens are separated by spaces. In
this example "a" is one allowed pattern, and alpha+ is the other.

I would like to trigger one particular action (let's say it prints
"keyword") when the pattern "a" has been recognized, and a different
action when the alpha+ pattern has been recognized (let's say it
prints "variable").

I've worked through most of the samples in the tutorial, but I'm still
stumped on this basic question:

How can I trigger these actions such that the action that prints
"keyword" is triggered for the input "a" but not for the input "abc",
and the action that prints "variable" is triggered for the input "abc"
but not for the input "a"?

Given the input "a bc abc def a gh a", I would like to see the following output:
keyword
variable
variable
variable
keyword
variable
keyword

My exact example is below. This is my first time posting to this list.
I appreciate any help or tips, and if this isn't the right place to
post please let me know. FWIW, I've tried using key @keyword, key
%keyword, key %*keyword, var @name, var %name, var %*name, and several
other user action triggers from chapter 3 of the manual.

#include <iostream>
#include <sstream>
#include <string.h>
#include <stdio.h>

void parse() {

char *p = "a bc abc def a gh a";
char *pe = p + strlen(p);
char *eof = p + strlen(p);
int cs;

%%{

machine minimal;
write data;

action keyword {
std::cerr << "keyword" << std::endl;
}

action name {
std::cerr << "variable" << std::endl;
}

key = "a";

var = alpha+;

whitespace = ' '*;

main := (( key | var ) whitespace )* ;

write init;
write exec;

}%%

}

int main() {
parse();
}


Thanks,
Lane
Adrian Thurston
2013-11-23 14:55:20 UTC
Permalink
Hi Lane,

The simplest way to do it is to exclude the keywords from the
identifiers using set difference. You can use (vars - keys).

You can also prioritize actions appropriately (keywords referenced
before idents) and break out of the action processing loop when you
execute the higher priority action.

You can also use a scanner.

-Adrian
Post by Lane Schwartz
I have a minimal example, where tokens are separated by spaces. In
this example "a" is one allowed pattern, and alpha+ is the other.
I would like to trigger one particular action (let's say it prints
"keyword") when the pattern "a" has been recognized, and a different
action when the alpha+ pattern has been recognized (let's say it
prints "variable").
I've worked through most of the samples in the tutorial, but I'm still
How can I trigger these actions such that the action that prints
"keyword" is triggered for the input "a" but not for the input "abc",
and the action that prints "variable" is triggered for the input "abc"
but not for the input "a"?
keyword
variable
variable
variable
keyword
variable
keyword
My exact example is below. This is my first time posting to this list.
I appreciate any help or tips, and if this isn't the right place to
other user action triggers from chapter 3 of the manual.
#include<iostream>
#include<sstream>
#include<string.h>
#include<stdio.h>
void parse() {
char *p = "a bc abc def a gh a";
char *pe = p + strlen(p);
char *eof = p + strlen(p);
int cs;
%%{
machine minimal;
write data;
action keyword {
std::cerr<< "keyword"<< std::endl;
}
action name {
std::cerr<< "variable"<< std::endl;
}
key = "a";
var = alpha+;
whitespace = ' '*;
main := (( key | var ) whitespace )* ;
write init;
write exec;
}%%
}
int main() {
parse();
}
Thanks,
Lane
_______________________________________________
ragel-users mailing list
ragel-users at complang.org
http://www.complang.org/mailman/listinfo/ragel-users
Loading...