[ragel-users] Keywords and actions in a minimal example

Lane Schwartz dowobeha at gmail.com
Tue Jun 25 18:20:00 UTC 2013

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:

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() {


ragel-users mailing list
ragel-users at complang.org

More information about the ragel-users mailing list